Skip to Content

How to sort numbers in SAPUI5?

Hi all,

I tried to sort custom list items in my Employee list app based on Employee Number, Name and Designation.

The list gets sorted correctly when I sort it by name and Designation. But it's not getting sorted properly when I sort  it by Employee number. I mean the employee number is not taken as single number. Instead, it's taken as character by character.So even if the number is small, it gets sorted at the last.

Can someone help me with this?

Here’s what I have tried:

View coding:

var radiobtn1=new sap.m.RadioButton("radiobtn1",{enabled:true,selected:false,text:"Firstname",select:function(){globalradio="FirstName"; }});

var radiobtn2=new sap.m.RadioButton("radiobtn2",{enabled:true,selected:false,text:"EmployeeId",select:function(){globalradio="EmployeeId";}});


var radiobtn3=new sap.m.RadioButton("radiobtn3",{enabled:true,selected:false,text:"Designation",select:function(){globalradio="Designation";}});

var sortbtn=new sap.m.Image("sortbtn",{src:"jpg pics/sort4.png",press:function(){

  if(globalradio == "FirstName")

  {

  oController.namesort();

  }

  else if(globalradio == "EmployeeId")

  {

  oController.empidsort();

  }

  else if(globalradio == "Designation")

  {

  oController.designsort();

  }

controller coding:

  namesort:function(oEvent){

            

            var sort_array=new Array();

            var f_sort=new sap.ui.model.Sorter("FirstName");

            sort_array.push(f_sort);

            this.name_list=sap.ui.getCore().byId("m_lvlist");

            this.name_list.getBinding("items").sort(sort_array);

             },

            

empidsort:function(oEvent){

            var sort_array1=new Array();

            var id_sort=new sap.ui.model.Sorter("EmpNum");

            sort_array1.push(id_sort);

            this.id_list=sap.ui.getCore().byId("m_lvlist");

            this.id_list.getBinding("items").sort(sort_array1);

             },

            

designsort:function(oEvent)

             {

            var sort_array2=new Array();

            var design_sort=new sap.ui.model.Sorter("Designation");

            sort_array2.push(design_sort);

            this.design_list=sap.ui.getCore().byId("m_lvlist");

            this.design_list.getBinding("items").sort(sort_array2);

             },

Thanks & Regards,

Ramya

screenshot.png (99.5 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Best Answer
    Nov 02, 2015 at 09:46 AM

    Thanks  all....

    My list gets sorted correctly now. The problem was with the Odata. In the web service model, they had kept the 'EmpNum' field as Character field. After changing it to numeric field, the list gets sorted properly based on Employee id. Here's my coding:

    empidsort:function(oEvent)

    {

    var sort_array1=new Array();

    var id_sort=new sap.ui.model.Sorter("EmpNum");

    sort_array1.push(id_sort);

    this.id_list=sap.ui.getCore().byId("m_lvlist");

    this.id_list.getBinding("items").sort(sort_array1);

    },

    Regards,

    Ramya

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Ramya,


      Great! Can you mark your last response as correct answer and close the thread. This will be helpful to others who have the same issue / problem in future.


      Thank you!

      Regards,

      Sai Vellanki.

  • Nov 02, 2015 at 06:51 AM

    Hi Ramya,

    Will this help? Plunker - ListSorting

    Click on Sort by Employee ID radio, it sorts with respect to Employee ID.

    Click on Sort by Name radio, it sorts with respect to Name.

    Regards,

    Sai Vellanki.

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Ramya,

      Try like this:

      var oSorter = new sap.ui.model.Sorter("EmpNum");
      var oList = sap.ui.getCore().byId("m_lvlist");
      var oBinding = oList.getBinding("items");
      oBinding.sort(oSorter);
      

      Just check in your model once, whether the field name that returns employee id is "EmpNum" or not.

      Regards,

      Sai Vellanki.

  • Oct 31, 2015 at 06:05 PM

    is your EmpNum a numeric value ? does it have any non-numeric characters in it? if it has any non-numeric chars, then that's the reason. if it is only numeric values, then your model should return it as a int, float, decimal and your sorter shouldn't be a problem... if this continues, then you may need to parse into the correct numeric value in order be be able to sort your EmpNum values. an alternative would be to use the sort function in JavaScript hope this helps

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 31, 2015 at 06:27 PM

    Could you please share your model data?

    Thanks,

    KA

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 31, 2015 at 07:20 PM

    var id_sort=new sap.ui.model.Sorter("EmpNum");

    id_sort.fnCompare = function(a, b){

      var intA = parseInt(a), intB = parseInt(b);

      if (intA == intB) {

      return 0;

      }

      if (intA < intB) {

      return -1;

      }

      if (intA > intB) {

      return 1;

      }

      return 0;

    };

    Add comment
    10|10000 characters needed characters exceeded