How to sort an List of object arrays in ascending order

1 answer

I have an hibernate entity class:

    @Entity     @Table(name = "grade")     @JsonIgnoreType     public class StudentMaster implements Serializable {      @Id     @GeneratedValue(strategy = GenerationType.IDENTITY)     @Column(name = "grade_id", unique = true, nullable = false)     private Integer gradeId;      @Column(name = "grade_name")     private String gradeName;     //getter and setter    } 

I have written a query that return a list of object array: String query = select T.gradeId, T.gradeName from grade where available = 1;

This return a list of object array

    List<Object[]> list = session.createQuery(query.toString()).list(); 

List is coming like

    [0]      >[0] = 5001      >[1] = "10"     [1]      >[0] = 5002      >[1] = "11"     [2]      >[0] = 5003      >[1] = "4"     [3]      >[0] = 5004      >[1] = "18"        and so on.... 

I am trying to sort this in ascending order like 4, 10, 11, 18 I tried with the below code snippet but didn't work

   Collections.sort(list, new Comparator<StudentMaster>() {         public int compare(StudentMaster s1, StudentMaster s2) {             return s1.getGradeName().compareTo(s2.getGradeName());         }     }); 

can someone help me here. I am learning myself and new to coding so need help. Thank you

All answers to this question, which has the identifier 61237223

The best answer:

I would suggest to let DB to do its best and sort it for you. Just add order by to your query.


Since you want to order by integer value stored as string, you need to convert it to integer, using TO_NUMBER or specific SQL dialect syntax. In-memory sorting could be implemented like this :

new Comparator<Object[]>() {  public int compare(Object[] o1, Object[] o2) {  return Integer.valueOf((String) o1[1]).compareTo(Integer.valueOf((String) o2[1]));  }  }); 

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data