cancel
Showing results for 
Search instead for 
Did you mean: 

How to sort numbers in SAPUI5?

former_member195820
Participant
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

former_member195820
Participant

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

saivellanki
Active Contributor
0 Kudos

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.

Answers (4)

Answers (4)

saivellanki
Active Contributor
0 Kudos

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.

former_member195820
Participant
0 Kudos

Hi Sai,

I tried the model which you gave me.But i get the following error:

Uncaught TypeError: Cannot read property 'getBinding' of undefined

Regards,

Ramya

saivellanki
Active Contributor
0 Kudos

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.

former_member182372
Active Contributor
0 Kudos

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;

};

former_member195820
Participant
0 Kudos

Hi Maksim,

I tried this.But it's not working.I get the same output.

Thanks,

Ramya

karthikarjun
Active Contributor
0 Kudos

Could you please share your model data?

karthikarjun
Active Contributor
0 Kudos

Could you please share your model data?

Thanks,

KA

former_member195820
Participant
0 Kudos
karthikarjun
Active Contributor
0 Kudos

Not this Ramya. Goto console--->thype sap.ui.getCore().getModel("YOUR MODEL ");

SergioG_TX
Active Contributor
0 Kudos

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

former_member195820
Participant
0 Kudos

Yes my EmpNum is a numeric value and doesnot contain any non-numeric characters.