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.
how about not using a Comparator
class and simply implementing bubble sorting ?
something like this,
for (int c = 0; c < (yourArrayList.size() - 1); c++) {
for (int d = 0; d < (yourArrayList.size() - c - 1); d++) {
if (Integer.parseInt(yourArrayList.get(d).get("number")) > Integer
.parseInt(yourArrayList.get(d + 1).get("number"))) {
temporary = yourArrayList.get(d);
yourArrayList.set(d, yourArrayList.get(d + 1));
yourArrayList.set(d + 1, temporary);
}
}
}
Look at this example,
import java.util.ArrayList;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
ArrayList<HashMap<String, String>> yourArrayList =
new ArrayList<HashMap<String, String>>();
HashMap<String, String> myHashMap = new HashMap<String, String>();
myHashMap.put("title", "first Title");
myHashMap.put("date", "This is 1st date");
myHashMap.put("number", "5");
yourArrayList.add(0, myHashMap);
myHashMap = new HashMap<String, String>();
myHashMap.put("title", "Second Title");
myHashMap.put("date", "This is 2nd date");
myHashMap.put("number", "2");
yourArrayList.add(1, myHashMap);
myHashMap = new HashMap<String, String>();
myHashMap.put("title", "Third Title");
myHashMap.put("date", "This is 3rd date");
myHashMap.put("number", "7");
yourArrayList.add(2, myHashMap);
myHashMap = new HashMap<String, String>();
myHashMap.put("title", "Fourth Title");
myHashMap.put("date", "This is 4th date");
myHashMap.put("number", "0");
yourArrayList.add(3, myHashMap);
System.out.println("=================");
System.out.println("BEFORE SORTING");
System.out.println("=================");
for (int i = 0; i < yourArrayList.size(); i++) {
System.out.println(yourArrayList.get(i));
}
HashMap<String, String> temporary;
for (int c = 0; c < (yourArrayList.size() - 1); c++) {
for (int d = 0; d < (yourArrayList.size() - c - 1); d++) {
if (Integer.parseInt(yourArrayList.get(d).get("number")) > Integer
.parseInt(yourArrayList.get(d + 1).get("number"))) {
temporary = yourArrayList.get(d);
yourArrayList.set(d, yourArrayList.get(d + 1));
yourArrayList.set(d + 1, temporary);
}
}
}
System.out.println("=================");
System.out.println("AFTER SORTING");
System.out.println("=================");
for (int i = 0; i < yourArrayList.size(); i++) {
System.out.println(yourArrayList.get(i));
}
}
}
Output,
=================
BEFORE SORTING
=================
{date=This is 1st date, number=5, title=first Title}
{date=This is 2nd date, number=2, title=Second Title}
{date=This is 3rd date, number=7, title=Third Title}
{date=This is 4th date, number=0, title=Fourth Title}
=================
AFTER SORTING
=================
{date=This is 4th date, number=0, title=Fourth Title}
{date=This is 2nd date, number=2, title=Second Title}
{date=This is 1st date, number=5, title=first Title}
{date=This is 3rd date, number=7, title=Third Title}
You can test it here -> http://ideone.com/E0XXoN