1、字符串排序
import java.util.*;
public class SortString {
public static void main(String[] args) {
reverseByAsckString();
}
//按照字符串按长度,由小到大排序输出
//输入
// 3
//adfasdf
//asdf
//adsfadfasdf
//输出
//asdf
//adfasdf
//adsfadfasdf
//Collections.sort的使用
public static void reverseByLengthString() {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine();
ArrayList list = new ArrayList<>();
for (int i = 0; i < n; i++) {
String line = scanner.nextLine();
if (line.equals("stop")) {
break;
} else {
list.add(line);
}
}
Collections.sort(list, new Comparator() {
@Override
public int compare(String o1, String o2) {
if (o1.length() < o2.length()) {
return -1;
} else {
return 1;
}
}
});
list.forEach(ele -> {
System.out.println(ele);
});
}
//将多个按照字符串asck码,由小到大排序输出
//输入
// 3
//asdf
//asdf
//qwerqwe
//输出
//asdf
//asdf
//qwerqwe
public static void reverseByAsckString() {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine();
ArrayList list = new ArrayList<>();
for (int i = 0; i < n; i++) {
String line = scanner.nextLine();
if (line.equals("stop")) {
break;
} else {
list.add(line);
}
}
Collections.sort(list, new Comparator() {
@Override
public int compare(String o1, String o2) {
if (o1.compareTo(o2)<0) {
return -1;
} else {
return 1;
}
}
});
list.forEach(ele -> {
System.out.println(ele);
});
}
//将一个字符串按照字符asck码的顺序升序排序后输出
// 输入bacd
// 输出abcd
//Arrays.sort的使用
public static void reverseByInnerAsckString() {
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
char[] chars = line.toCharArray();
Character[] character= new Character[chars.length];
for (int i = 0; i < chars.length; i++) {
character[i]=chars[i];
}
Arrays.sort(character, new Comparator() {
@Override
public int compare(Character o1, Character o2) {
return o1-o2;
}
});
for (int i = 0; i < character.length; i++) {
chars[i]=character[i];
}
System.out.print(chars);
}
}
2、map排序
import java.util.*;
public class SortMap {
public static void main(String[] args) {
// Map stringIntegerMap = sortMapByKey();
//
// Set> entries = stringIntegerMap.entrySet();
// entries.forEach(ele->{
// System.out.println(ele.getKey()+','+ele.getValue());
// });
sortMapByValue();
}
public static Map sortMapByKey() {
Map stu = new TreeMap<>(new Comparator() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
//传进来一个key的比较器对象来构造treemap
stu.put("apple", 55);
stu.put("boy", 32);
stu.put("cat", 22);
stu.put("dog", 12);
stu.put("egg", 11);
return stu;
}
public static Map sortMapByValue() {
Map stu = new HashMap();
//传进来一个key的比较器对象来构造treemap
stu.put("apple", 55);
stu.put("boy", 32);
stu.put("cat", 22);
stu.put("dog", 22);
stu.put("egg", 11);
Set> entries = stu.entrySet();
ArrayList> arrayList = new ArrayList<>(entries);
Collections.sort(arrayList, new Comparator>() {
@Override
public int compare(Map.Entry o1, Map.Entry o2) {
if(o1.getValue()>o2.getValue()){
return 1;
}
if(o1.getValue().equals(o2.getValue())){
if(o1.getKey().compareTo(o2.getKey())>0){
return 1;
}
if(o1.getKey().compareTo(o2.getKey())==0){
return 0;
}
if(o1.getKey().compareTo(o2.getKey())<0){
return -1;
}
}
if(o1.getValue(){
System.out.println(ele.getKey()+','+ele.getValue());
});
return stu;
}
}



