There are many question related to this which suggests to use Comparator to compare and sort data, and I am already trying it and struggling to make it work so please don't report it as duplicate.
I have an arraylist of HashMap<String, String>
ArrayList<HashMap<String, String>>
and having data in this list in this form,
title , link and number are keys.
{ {title="",link="",number=}, {title="",link="",number=}, {title="",link="",number=} }
Example,
{ {title,link,number = 8}, {title,link,number = 1}, {title,link,number = 3} }
should be changed to,
{ {title,link,number = 1}, {title,link,number = 3}, {title,link,number = 8} }
and I wanted to sort it based on the number, I have tried this,
I create a new class (as suggested in many post to create new class to compare data) which implements Comparator.
public class SortData implements Comparator<ArrayList<HashMap<String, String>>>
the method which is automatically implemented is ,
@Override
public int compare(ArrayList<HashMap<String, String>> lhs,
ArrayList<HashMap<String, String>> rhs) {
return 0;
}
Now this method suggest to use two arraylist of Hashmap to compare, but since I have only one arraylist which needs to be sorted so what should i use for the second arraylist ?
my Arraylist name is SecondArray, and I want to compare each value of it with the next value,
@Override
public int compare(ArrayList<HashMap<String, String>> lhs,
ArrayList<HashMap<String, String>> rhs) {
lhs = SecondArray;
rhs = // How to compare to the next value of the same Array ?
return 0;
}
How should I compare the same arraylist with the next value ?
Update: each Array list element has three key/value pairs, one of them is a number , I want to sort the arraylist based on that number , which means, key/value pairs which has the lowest number should be first in the array list.
{ {title,link,number}, {title,link,number}, {title,link,number} }? Maps are key=value...