[Link]
com/blog/2008/10/16/sorting-an-arraylist-of-objects/
class Person {
private String firstName;
private String lastName;
public Person(){
}
public Person(String firstName, String lastName){
[Link] = firstName;
[Link] = lastName;
}
/**
* @return Returns the firstName.
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName The firstName to set.
*/
public void setFirstName(String firstName) {
[Link] = firstName;
}
/**
* @return Returns the lastName.
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName The lastName to set.
*/
public void setLastName(String lastName) {
[Link] = lastName;
}
public String toString(){
return "## Firstname : "+[Link]+", Lastname :
"+[Link];
}
}
I have created some objects of Person class and they are added to an ArrayList. Now I
need to sort the ArrayList based on Person’s firstName. The complete code listing for
sorting the ArrayList is given below.
Person p = new Person("Bruce", "Willis");
Person p1 = new Person("Tom", "Hanks");
Person p2 = new Person("Nicolas","Cage");
Person p3 = new Person("John","Travolta");
ArrayList list = new ArrayList();
[Link](p);
[Link](p1);
[Link](p2);
[Link](p3);
[Link](list, new Comparator(){
public int compare(Object o1, Object o2) {
Person p1 = (Person) o1;
Person p2 = (Person) o2;
return
[Link]().compareToIgnoreCase([Link]());
}
});
[Link](list);
[Link]
[Link]
public static void sortAnyList(){
ArrayList aList = new ArrayList();
[Link]("Danny");
[Link]("Maddy");
[Link]("Rahul");
[Link]("Nilesh");
[Link]("Pranjna");
[Link]("Rohit");
[Link](aList, new Comparator()
{
public int compare(Object o1, Object o2)
{
String s1 = (String)o1;
String s2 = (String)o2;
return [Link](s2);
}
});
}
[Link]
public class StringLengthComparator implements Comparator<String> {
public int compare(String o1, String o2) {
if ([Link]() < [Link]()) {
return -1;
} else if ([Link]() > [Link]()) {
return 1;
} else {
return 0;
}
}
}
Now, armed with our Comparator and some vital string data that we need
ordering by length:
String[] strs = {"boxer shorts", "grundies", "boxers",
"elasticated Y-fronts", "underpants", "briefs"};
we can sort our string data by length with a simple call to [Link](), this
time passing in an instance of our parameter:
// Sort
[Link](strs, new StringLengthComparator());
// Print the results
for (String s : strs)
[Link](s);
[Link]
1. import [Link].*;
2.
3. public class Test {
4.
5. public static void main(String[] args) {
6. ArrayList list = new ArrayList();
7.
8. [Link]("A");
9. [Link]("H");
10. [Link]("L1");
11. [Link]("L100");
12. [Link]("L11");
13. [Link]("L2");
14. [Link]("L3");
15. [Link]("X");
16.
17. class MyComparator implements Comparator {
18.
19. public int compare(Object o1,
20. Object o2) {
21. String s1 = [Link]();
22. String s2 = [Link]();
23.
24. int counter = 0;
25.
26. for (counter=0;counter<[Link]() &&
27. !
[Link]([Link](counter));counter++);
28.
29. String temp1 = [Link](counter);
30.
31. s1 = [Link](0,counter);
32.
33. for (counter=0;counter<[Link]() &&
34. !
[Link]([Link](counter));counter++);
35.
36. String temp2 = [Link](counter);
37.
38. s2 = [Link](0,counter);
39.
40. int max = ([Link]() > [Link]()) ? [Link]()
: [Link]();
41.
42. char[] pad = new char[[Link]()];
43.
44. [Link](pad,(char)48);
45.
46. temp1 = [Link](pad) + temp1;
47.
48. pad = new char[[Link]()];
49.
50. [Link](pad,(char)48);
51.
52. temp2 = [Link](pad) + temp2;
53.
54. s1 = s1 + temp1;
55. s2 = s2 + temp2;
56.
57. return([Link](s2));
58. }
59.
60. public boolean equals(Object o1,
61. Object o2) {
62. String s1 = [Link]();
63. String s2 = [Link]();
64.
65. return([Link](s2));
66. }
67.
68. }
69.
70. [Link](list,new MyComparator());
71.
72. Iterator iterator = [Link]();
73.
74. while ([Link]())
75. [Link]([Link]());
76. }
77.
78. }
[Link]
1. /*
2. Sort elements of Java ArrayList Example
3. This Java Example shows how to sort the elements of java ArrayList
object using
4. [Link] method.
5. */
6.
7. import [Link];
8. import [Link];
9.
10. public class SortJavaArrayListExample {
11.
12. public static void main(String[] args) {
13.
14. //create an ArrayList object
15. ArrayList arrayList = new ArrayList();
16.
17. //Add elements to Arraylist
18. [Link]("1");
19. [Link]("3");
20. [Link]("5");
21. [Link]("2");
22. [Link]("4");
23.
24. /*
25. To sort an ArrayList object, use [Link] method. This is
a
26. static method. It sorts an ArrayList object's elements into
ascending order.
27. */
28. [Link](arrayList);
29.
30. //display elements of ArrayList
31. [Link]("ArrayList elements after sorting in ascending
order : ");
32. for(int i=0; i<[Link](); i++)
33. [Link]([Link](i));
34.
35. }
36. }
37.
38. /*
39. Output would be
40. ArrayList elements after sorting in ascending order :
41. 1
42. 2
43. 3
44. 4
45. 5
46. */
HashMap sort
Method 1
1. HashMap map = new HashMap();
2. [Link]("0201", "0201");
3. [Link]("01", "01");
4. [Link]("0304", "0304");
5. [Link]("0101", "0101");
6.
7. Object[] key = [Link]().toArray();
8. [Link](key);
9.
10. for (int i = 0; i < [Link]; i++)
{
11. [Link]([Link](key[i]));
12. }
Method 2
public HashMap getSortedMap(HashMap hmap)
{
HashMap map = new LinkedHashMap();
List mapKeys = new ArrayList([Link]());
List mapValues = new ArrayList([Link]());
[Link]();
TreeSet sortedSet = new TreeSet(mapValues);
Object[] sortedArray = [Link]();
int size = [Link];
// a) Ascending sort
for (int i=0; i<size; i++)
{
[Link]([Link]([Link](sortedArray[i])), sortedArray[i]);
}
return map;
}
Method 3
static Map sortByValue(Map map) {
List list = new LinkedList([Link]());
[Link](list, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) (([Link]) (o1)).getValue())
.compareTo((([Link]) (o2)).getValue());
}
});
// [Link](list);
Map result = new LinkedHashMap();
for (Iterator it = [Link](); [Link]();) {
[Link] entry = ([Link])[Link]();
[Link]([Link](), [Link]());
}
return result;
}
Method 4
public LinkedHashMap sortHashMapByValuesD(HashMap passedMap) {
List mapKeys = new ArrayList([Link]());
List mapValues = new ArrayList([Link]());
[Link](mapValues);
[Link](mapKeys);
LinkedHashMap sortedMap =
new LinkedHashMap();
Iterator valueIt = [Link]();
while ([Link]()) {
Object val = [Link]();
Iterator keyIt = [Link]();
while ([Link]()) {
Object key = [Link]();
String comp1 = [Link](key).toString();
String comp2 = [Link]();
if ([Link](comp2)){
[Link](key);
[Link](key);
[Link]((String)key, (Double)val);
break;
return sortedMap;
Method 5:
HashMap based on values :
HashMap map1 = new HashMap();
[Link]("cat",5);
[Link]("cow",4);
[Link]("dog",3);
[Link]("horse",2);
List mpKeys = new ArrayList([Link]());
List mpValues = new ArrayList([Link]());
HashMap map = new LinkedHashMap();
TreeSet sortedSet = new TreeSet(mpValues);
Object[] sortedArray = [Link]();
int size = [Link];
// descending
for (int i=size-1; i>=0; i--)
[Link]([Link]([Link](sortedArray[i]))+"
"+sortedArray[i]);
Based on Key:
Map myMap = new HashMap();
// put keys and values........
Map sortedMap = new TreeMap(myMap);
Method 6
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
HashMap callList = new HashMap();
[Link]("java", "pavan");
[Link]("makes", "srinivas");
[Link]("life", "sample");
[Link]("easy", "java");
Set set=[Link]();
String[] keys=new String[[Link]()];
[Link](keys);
List tmpkeyList=[Link](keys);
[Link](tmpkeyList);
for(String key:tmpkeyList){
[Link](key+":"+[Link](key));
}
}
}
Method 7
sort based on the keys
Map yourMap= new HashMap();
// put some tuples in yourMap ...
Map sortedMap = new TreeMap(yourMap);
Sort based on the values
HashMap yourMap = new HashMap();
// put some tuples in yourMap ...
// to hold the result
HashMap map = new LinkedHashMap();
List yourMapKeys = new ArrayList([Link]());
List yourMapValues = new ArrayList([Link]());
TreeSet sortedSet = new TreeSet(yourMapValues);
Object[] sortedArray = [Link]();
int size = [Link];
for (int i=0; i<size; i++) {
[Link]
([Link]([Link](sortedArray[i])),
sortedArray[i]);
}
To iterate your new Sorted Map
Set ref = [Link]();
Iterator it = [Link]();
while ([Link]()) {
String file = (String)[Link]();
}