Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

sortby  RSS feed

 
naveen gupta
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am using sorting for the list in datatable, using property "sortby"

for example, if i have following records

932
1122
912
1234


While doing ascending order, it sorts as follows

1122
1234
912
932

which is not correct, 912 and 932 should appear first. How should i resolve this problem??





 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naveen,
Based on the information you have provided, I'm guessing you are sorting strings and not numbers.
If my guess is correct then I know of two possibilities available to you.
  • Change the strings to numbers.
  • Keep as strings but insert leading zeros.

  • Using the second option, your list would appear as:

    0912
    0932
    1122
    1234

    Hope this helps.

    Good Luck,
    Avi.
     
    naveen gupta
    Ranch Hand
    Posts: 129
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    yeah, i think you are right. I will check that. thanks
     
    naveen gupta
    Ranch Hand
    Posts: 129
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yeah, because the variable is of String type

    Now i am looking for solution to solve this

    I have to define this variable as "String" only and i can't append 0's to the value

    Is there any other solution for this problem

    like while displaying or while sorting can we tell it to consider the values as Number???
     
    Avi Abrami
    Ranch Hand
    Posts: 1141
    1
    Java Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Naveen,
    You asked:

    Is there any other solution for this problem like while displaying or while sorting can we tell it to consider the values as Number?

    I suggest method sort in class java.util.Collections.

    Good Luck,
    Avi.
     
    naveen gupta
    Ranch Hand
    Posts: 129
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    yeah, but again i have to use the same in Java code

    But i want some way to interpret the string as Integer in the JSF code itself
     
    Guy deLyonesse
    Ranch Hand
    Posts: 200
    Eclipse IDE Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your best bet is to sort the items in the backing bean and then display them on your page. On your view, are you using some kind of list?
     
    naveen gupta
    Ranch Hand
    Posts: 129
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    yes, i am using ArrayList on my view page

    i am using sortBy attribute to sort, the feature provided by RichFaces for dataTable

    anyway, i have changed type from String to Integer
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!