# 请教一个list字符排序问题

xiaoqiangse 2020-08-13 04:24:33
List<String> list=new ArrayList<>();

'1-1','1-2','1-10','2-1','2-10-1','2-10-2','2-10-10','3-1'

...全文
429 点赞 收藏 7

7 条回复

ZMXQQ233 2020-08-13
List<String> list=new ArrayList<>(); int[] num = {}; list.add("1-1"); list.add("1-2"); for (int i = 0; i < list.size(); i++) {//存在-转为. if (list.get(i).indexOf("-") != -1){ list.get(i).replace('-','.'); } } for (int i = 0; i < list.size(); i++) {//对.之前的数进行存储与排序 if (list.get(i).indexOf(".") != -1){ num[i] = Integer.parseInt(list.get(i).substring(0,list.get(i).indexOf("."))); //这里进行排序 } } for (int i = 0; i < list.size(); i++) { System.out.println(list); } 我的想法就是先转成点， 再排序。 具体代码怎么优化你再看看

``````
public void test(){
List<String> list=new ArrayList<>();
list.sort(Comparator.comparing(Function.identity(), this::compare));
System.out.println(list);
}

public int compare(String x, String y){
String[] split = x.split("-");
String[] split1 = y.split("-");
int min = Math.min(split.length, split1.length);
for (int i = 0; i < min; i++) {
Integer s0 = Integer.valueOf(split[i]);
Integer s1 = Integer.valueOf(split1[i]);
if (!s0.equals(s1)){
return s0.compareTo(s1);
}
}
return min==split.length?-1:1;
}

``````

xiong_zai_ 2020-08-13
``````list.sort((o1, o2) -> {
String[] a1 = o1.split("-");
String[] a2 = o2.split("-");
int l = Integer.max(a1.length, a2.length);
int i1 = 0;
int i2 = 0;
for (int i = 0; i < l; i++) {
i1 = i > a1.length - 1 ? 0 : Integer.parseInt(a1[i]);
i2 = i > a2.length - 1 ? 0 : Integer.parseInt(a2[i]);
if (i1 != i2) {
break;
}
}
return i1 - i2;
});``````

dkwuxiang 2020-08-13

Motivation丶R 2020-08-13

``````public static void main(String[] args) {
List<String> list = new ArrayList<>();

List<Node> sortList = list.stream().map(item -> {
String s = item.replaceFirst("-", ".").replaceAll("-", "");
return new Node(Double.parseDouble(s),item);
}).collect(Collectors.toList());
Collections.sort(sortList, Comparator.comparing(Node::getValue));
list = sortList.stream().map(item -> item.getKey()).collect(Collectors.toList());

list.stream().forEach(item -> System.out.println(item));
}

@Data
static class Node{
private Double value;
private String key;
public Node(Double value,String key){
this.key = key;
this.value = value;
}
}``````

`````` public static void main(String[] args) {
List<String> list = new ArrayList<>();

Collections.sort(list,(item1,item2) ->{
String s1 = item1.replaceFirst("-", ".").replaceAll("-", "");
String s2 = item1.replaceFirst("-", ".").replaceAll("-", "");;
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
return d1 - d2 > 0 ? 1 : d1 == d2 ? 0 : -1;
});
list.stream().forEach(item -> System.out.println(item));
}``````

Motivation丶R 2020-08-13
``````public static void main(String[] args) {
List<String> list = new ArrayList<>();

List<Node> sortList = list.stream().map(item -> {
String s = item.replaceFirst("-", ".").replaceAll("-", "");
return new Node(Double.parseDouble(s),item);
}).collect(Collectors.toList());
Collections.sort(sortList, Comparator.comparing(Node::getValue));
list = sortList.stream().map(item -> item.getKey()).collect(Collectors.toList());

list.stream().forEach(item -> System.out.println(item));
}

@Data
static class Node{
private Double value;
private String key;
public Node(Double value,String key){
this.key = key;
this.value = value;
}
}``````

tianfang 2020-08-13

Java EE

6.7w+

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器，保护我们的业务核心组件（中间件），以延续它的生命力，而不是依赖J2SE/J2EE版本。

2020-08-13 04:24