-2

I have a Model Class and I am trying to Sort The ArrayList list according to the ID in Model Class. How can I get a soretd list in a simple way ?

My Model Class includes :

ID :      Name :
1         Apple
2         Microsoft
3         Samsung
4         Lenovo
5         Mi
Koyas
  • 98
  • 1
  • 11
  • Check out the sorting feature of Java Streams – RobOhRob Jun 17 '19 at 14:26
  • 3
    Possible duplicate of [Sort Java Collection](https://stackoverflow.com/questions/6957631/sort-java-collection). I downvoted because [no apparent research](https://idownvotedbecau.se/noresearch/) was done. – Austin Schaefer Jun 17 '19 at 14:26
  • @Austin, I am new with Android & java platforms. Also i have less experience with programming. – Koyas Jun 17 '19 at 16:16

1 Answers1

2

You can sort any collection in java by this code:

Collections.sort(mList, new Comparator<YourModel>(){
    public int compare(YourModel obj1, YourModel obj2) {
        return Integer.valueOf(obj1.ID).compareTo(Integer.valueOf(obj2.ID));
        // Or you can compare any field of your object
    }
});

for more detail go to docs

kAvEh
  • 534
  • 4
  • 15
  • 1
    thanks. Actually I was ignorant about Collections.sort can be used this way. I was discovered my own method to sort by Index according to ID. – Koyas Jun 17 '19 at 17:06