cancel
Showing results for 
Search instead for 
Did you mean: 

Sorting of Numbers in XML View is not working

Firoz_Ashraf
Contributor
0 Kudos
1,648

Hi,

Sorting of Numbers in my XML View is not working.

I have searched the forum and got to know that JSON treats all as characters.

Some post were referencing to use sap.ui.model.Sorter and fnCompare

My view is an XML view. How do I use this or is there any other solution?

Here is my XML Code for table column on which I want the sorting.



<Table:Column hAlign="Right" width="10em" sortProperty="Kwmeng" filterProperty="Kwmeng">

     <Table:label>

          <Label text="Order Quantity" />

     </Table:label>

     <Table:template>

          <Label text="{  path:'SalesModel>Kwmeng',type:'sap.ui.model.type.Float'}" />

     </Table:template>

</Table:Column>

Any help would be highly appreciated.

Regards,

Firoz.

carolinaoleitte
Newcomer
0 Kudos

Try this:

Set the event "sort" to your ui Table: 

<ui:Table
         id="myTable"
         selectionMode="MultiToggle"
         rows="{control>/Blablabla/results}"
         threshold="15"
         ariaLabelledBy="title"
         width="100%"
         sort="onTableSort"
         rowSelectionChange="onSelectTable"
>
 
Controller: 
 
onTableSort: function (oEvent) {
        var idColunaSelecionada = oEvent.mParameters.column.sId,
        colunaSelecionada = oEvent.getParameter("column"),
        direcao = oEvent.mParameters.sortOrder,
        oSorter;

        if (idColunaSelecionada === "container-myapp.test---Main--field1") {
               colunaSelecionada.setSortOrder(direcao);
               oEvent.preventDefault();
               that._resetSortingState();

               oSorter = new Sorter(colunaSelecionada.getSortProperty(), direcao === SortOrder.Descending);
               oSorter.fnCompare = function (a, b) {
                        var value1 = parseFloat(a),
                        value2 = parseFloat(b);

                        if (value1 == value2) {
                            return 0;
                        }
 
                       if (value1 < value2) {
                          return -1;
                       }
 
                       if (value1 > value2) {
                          return 1;
                        }
                        return 0;
         };

         oTable.getBinding().sort(oSorter);
}
},

_resetSortingState: function() {
         var aColumns = oTable.getColumns();
         for (let i = 0; i < aColumns.length; i++) {
              aColumns[i].setSortOrder(SortOrder.None);
          } 
}
View Entire Topic
saivellanki
Active Contributor
0 Kudos

Hi Firoz,

You might be having it in string format. Try converting to float using parseFloat() before binding.

Will this sample help? Plunker


Regards,

Sai Vellanki.

Firoz_Ashraf
Contributor
0 Kudos

Thanks Sai,

As suggested by you I converted the string format using parseFloat() and sorting issue got solved !

Here is my code for conversion:


oModel.read('/openorderSet',null,[fltr],false,function(oData){

  for (var i = 0; i < oData.results.length; i++) {

             var oOrdQty = parseFloat(oData.results[i].Kwmeng);

             oData.results[i].Kwmeng = oOrdQty;

           }

  oJsonModel.setData(oData);

  sap.ui.getCore().setModel(oJsonModel, "SalesModel");

  },function(){

                alert("Read failed for order item"); 

                });

Many Thanks,

Firoz.