Быстрый обзор сортировки Java:
обычный вид списка:
1
2
3
4
|
private static List VEGETABLES = Arrays.asList( "apple" , "cocumbers" , "blackberry" ); Collections.sort(VEGETABLES); output: apple, blackberry, cocumbers |
Обратная сортировка:
1
2
3
|
private static List VEGETABLES = Arrays.asList( "apple" , "cocumbers" , "blackberry" ); Collections.sort(VEGETABLES, Collections.reverseOrder()); output: cocumbers, blackberry, apple |
с пользовательским компаратором:
01
02
03
04
05
06
07
08
09
10
|
private class StringComparator implements Comparator { public int compare(Object o1, Object o2) { String so1 = (String) o1; String so2 = (String) o2; return so1.compareTo(so2); } } private static List VEGETABLES = Arrays.asList( "apple" , "cocumbers" , "blackberry" ); Collections.sort(VEGETABLES, new StringComparator()); output: apple, blackberry, cocumbers |
Сортировка элементов:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
private class Element implements Comparable { private String name; private Double atomicMass; @Override public String toString() { final StringBuilder sb = new StringBuilder(); sb.append( "Element" ); sb.append( "{name='" ).append(name).append('\ '' ); sb.append( ", atomicMass=" ).append(atomicMass); sb.append( '}' ); return sb.toString(); } public String getName() { return name; } public void setName(String name) { this .name = name; } public Double getAtomicMass() { return atomicMass; } public void setAtomicMass(Double atomicMass) { this .atomicMass = atomicMass; } public Element(String name, String mass, double atomicMass) { this .name = name; this .atomicMass = atomicMass; } public int compareTo(Element o) { return this .getAtomicMass().compareTo(o.getAtomicMass()); } } ArrayList<Element> elements = new ArrayList<Element>(); elements.add( new Element( "Hydrogen" , "H" , 1.00794 )); // Hydrogen 1.00794 amu Atomic Mass elements.add( new Element( "Iron" , "Fe" , 55.845 )); elements.add( new Element( "Lithium" , "Li" , 6.941 )); elements.add( new Element( "Lead" , "Pb" , 207.2 )); elements.add( new Element( "Magnesium" , "Mg" , 24.305 )); Collections.sort(elements); // Sort by Element |
выход:
1
2
3
4
5
|
Element{name= 'Hydrogen' , atomicMass=1.00794} Element{name= 'Lithium' , atomicMass=6.941} Element{name= 'Magnesium' , atomicMass=24.305} Element{name= 'Iron' , atomicMass=55.845} Element{name= 'Lead' , atomicMass=207.2} |
Хронологическая сортировка:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
|
SimpleDateFormat formatter = new SimpleDateFormat( "MMMM dd, yyyy" , Locale.US); try { ArrayList<Date> holidays = new ArrayList<Date>(); holidays.add(formatter.parse( "May 31, 2010" )); // Memorial Day holidays.add(formatter.parse( "July 4, 2010" )); // Independence Day holidays.add(formatter.parse( "February 15, 2010" )); // Presidents Day holidays.add(formatter.parse( "September 6, 2010" )); // Labor Day holidays.add(formatter.parse( "December 24, 2010" )); // Thanksgiving Day holidays.add(formatter.parse( "July 5, 2010" )); // federal employees extra day off for July 4th holidays.add(formatter.parse( "January 18, 2010" )); // Martin Luther King Day holidays.add(formatter.parse( "November 25, 2010" )); // federal employees extra day off for Christmas holidays.add(formatter.parse( "October 11, 2010" )); // Columbus Day holidays.add(formatter.parse( "December 25, 2010" )); // Christmas Day holidays.add(formatter.parse( "January 1, 2010" )); // New Year's Day Collections.sort(holidays); // Native sort for Date is chronological } catch (ParseException e) { e.printStackTrace(); } |
выход:
1
|
sorted:[Fri Jan 01 00:00:00 CET 2010, Mon Jan 18 00:00:00 CET 2010, Mon Feb 15 00:00:00 CET 2010, Mon May 31 00:00:00 CEST 2010, Sun Jul 04 00:00:00 CEST 2010, Mon Jul 05 00:00:00 CEST 2010, Mon Sep 06 00:00:00 CEST 2010, Mon Oct 11 00:00:00 CEST 2010, Thu Nov 25 00:00:00 CET 2010, Fri Dec 24 00:00:00 CET 2010, Sat Dec 25 00:00:00 CET 2010] |
Вы можете просмотреть полный простой класс ниже:
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
package com.tommyalf.personal.sorting; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; /** * Created by IntelliJ IDEA. * User: tommyalf * Date: 1-dic-2010 * Time: 22.40.49 */ public class SortDemo { private static List VEGETABLES = Arrays.asList( "apple" , "cocumbers" , "blackberry" );; public static void main(String args[]) { SortDemo sd = new SortDemo(); sd.normalSort(); sd.reverseSort(); sd.stringComparator(); sd.elementsSort(); sd.chronologicalSort(); } private void chronologicalSort() { SimpleDateFormat formatter = new SimpleDateFormat( "MMMM dd, yyyy" , Locale.US); try { ArrayList<Date> holidays = new ArrayList<Date>(); holidays.add(formatter.parse( "May 31, 2010" )); // Memorial Day holidays.add(formatter.parse( "July 4, 2010" )); // Independence Day holidays.add(formatter.parse( "February 15, 2010" )); // Presidents Day holidays.add(formatter.parse( "September 6, 2010" )); // Labor Day holidays.add(formatter.parse( "December 24, 2010" )); // Thanksgiving Day holidays.add(formatter.parse( "July 5, 2010" )); // federal employees extra day off for July 4th holidays.add(formatter.parse( "January 18, 2010" )); // Martin Luther King Day holidays.add(formatter.parse( "November 25, 2010" )); // federal employees extra day off for Christmas holidays.add(formatter.parse( "October 11, 2010" )); // Columbus Day holidays.add(formatter.parse( "December 25, 2010" )); // Christmas Day holidays.add(formatter.parse( "January 1, 2010" )); // New Year's Day System.out.println( "before sort:" + holidays); Collections.sort(holidays); // Native sort for Date is chronological System.out.println( "sorted:" + holidays); } catch (ParseException e) { e.printStackTrace(); } } private void elementsSort() { ArrayList<Element> elements = new ArrayList<Element>(); elements.add( new Element( "Hydrogen" , "H" , 1.00794 )); // Hydrogen 1.00794 amu Atomic Mass elements.add( new Element( "Iron" , "Fe" , 55.845 )); elements.add( new Element( "Lithium" , "Li" , 6.941 )); elements.add( new Element( "Lead" , "Pb" , 207.2 )); elements.add( new Element( "Magnesium" , "Mg" , 24.305 )); Collections.sort(elements); // Sort by Element System.out.print( "Elements sort by atomicMass value:" ); for ( Element e : elements ) { System.out.println(e); } } private void stringComparator() { Collections.sort(VEGETABLES, new StringComparator()); System.out.print( "StringComparator:" ); printList(VEGETABLES); } private void reverseSort() { Collections.sort(VEGETABLES, Collections.reverseOrder()); System.out.print( "ReverseSort:" ); printList(VEGETABLES); } private void normalSort() { Collections.sort(VEGETABLES); System.out.print( "NormalSort:" ); printList(VEGETABLES); } private void printList(List vegetables) { for ( int i = 0 , n = vegetables.size(); i < n; i++) { if (i != 0 ) { System.out.print( ", " ); } System.out.print(VEGETABLES.get(i)); } System.out.println(); } private class StringComparator implements Comparator { public int compare(Object o1, Object o2) { String so1 = (String) o1; String so2 = (String) o2; return so1.compareTo(so2); } } private class Element implements Comparable<Element> { private String name; private Double atomicMass; @Override public String toString() { final StringBuilder sb = new StringBuilder(); sb.append( "Element" ); sb.append( "{name='" ).append(name).append('\ '' ); sb.append( ", atomicMass=" ).append(atomicMass); sb.append( '}' ); return sb.toString(); } public String getName() { return name; } public void setName(String name) { this .name = name; } public Double getAtomicMass() { return atomicMass; } public void setAtomicMass(Double atomicMass) { this .atomicMass = atomicMass; } public Element(String name, String mass, double atomicMass) { this .name = name; this .atomicMass = atomicMass; } public int compareTo(Element o) { return this .getAtomicMass().compareTo(o.getAtomicMass()); } } } |
Ссылка: Пять полезных способов сортировки в java от нашего партнера JCG Томми Альфа в