Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

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 (99506 B)
Tags:
replied

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

1 View this answer in context
Not what you were looking for? View more on this topic or Ask a question