多个字符串排序java版
1 package com.defaultpackage;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 public class Test {
7 public static void main(String[] args) {
8 Test test = new Test();
9 ArrayList<String> source = new ArrayList<String>();
10 source.add("ac");
11 source.add("abc");
12 source.add("bac");
13 source.add("ab");
14 source.add("ca");
15 source.add("bc");
16 source.add("aaf");
17 source.add("bcf");
18 source.add("def");
19 source.add("dac");
20 source.add("aaced");
21 source.add("abacd");
22
23 List<char[]> rel = test.string_char(source);
24 int max = test.maxSize(rel);
25 test.firstSort(rel, 0, max);
26 for (char[] c : rel)
27 System.out.println(c);
28 }
29
30 private List<char[]> string_char(List<String> source) {
31 List<char[]> rel = new ArrayList<char[]>();
32 for (String str : source) {
33 char[] tem = str.toCharArray();
34 rel.add(tem);
35 }
36 return rel;
37 }
38
39 private int maxSize(List<char[]> cList) {
40 int i = 0;
41 for (char[] c : cList) {
42 if (c.length > i) {
43 i = c.length;
44 }
45 }
46 return i;
47 }
48
49 public void firstSort(List<char[]> source, int position, int max) {
50 char[] tem = null;
51 for (int i = 0; i < source.size(); i++) {
52 for (int j = 0; j < source.size() - i - 1; j++) {
53 if (position > 0) {
54 if (source.get(j).length <= position
55 || source.get(j + 1).length <= position) {
56 continue;
57 }
58 if (source.get(j)[position - 1] != source.get(j + 1)[position - 1]) {
59 continue;
60 }
61 }
62 if (source.get(j)[position] > source.get(j + 1)[position]) {
63 tem = source.get(j + 1).clone();
64 source.remove(j + 1);
65 source.add(j + 1, source.get(j));
66 source.remove(j);
67 source.add(j, tem);
68 }
69 if (source.get(j)[position] == source.get(j + 1)[position]) {
70 if (source.get(j).length > source.get(j + 1).length) {
71 tem = source.get(j + 1).clone();
72 source.remove(j + 1);
73 source.add(j + 1, source.get(j));
74 source.remove(j);
75 source.add(j, tem);
76 }
77 }
78 }
79 }
80 // for(char[] c : source){
81 // System.out.println(c);
82 // }
83 // System.out.println("========================================position="+position);
84 if (position < max) {
85 firstSort(source, position + 1 , max);
86 }
87
88 }
89
90 }
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 public class Test {
7 public static void main(String[] args) {
8 Test test = new Test();
9 ArrayList<String> source = new ArrayList<String>();
10 source.add("ac");
11 source.add("abc");
12 source.add("bac");
13 source.add("ab");
14 source.add("ca");
15 source.add("bc");
16 source.add("aaf");
17 source.add("bcf");
18 source.add("def");
19 source.add("dac");
20 source.add("aaced");
21 source.add("abacd");
22
23 List<char[]> rel = test.string_char(source);
24 int max = test.maxSize(rel);
25 test.firstSort(rel, 0, max);
26 for (char[] c : rel)
27 System.out.println(c);
28 }
29
30 private List<char[]> string_char(List<String> source) {
31 List<char[]> rel = new ArrayList<char[]>();
32 for (String str : source) {
33 char[] tem = str.toCharArray();
34 rel.add(tem);
35 }
36 return rel;
37 }
38
39 private int maxSize(List<char[]> cList) {
40 int i = 0;
41 for (char[] c : cList) {
42 if (c.length > i) {
43 i = c.length;
44 }
45 }
46 return i;
47 }
48
49 public void firstSort(List<char[]> source, int position, int max) {
50 char[] tem = null;
51 for (int i = 0; i < source.size(); i++) {
52 for (int j = 0; j < source.size() - i - 1; j++) {
53 if (position > 0) {
54 if (source.get(j).length <= position
55 || source.get(j + 1).length <= position) {
56 continue;
57 }
58 if (source.get(j)[position - 1] != source.get(j + 1)[position - 1]) {
59 continue;
60 }
61 }
62 if (source.get(j)[position] > source.get(j + 1)[position]) {
63 tem = source.get(j + 1).clone();
64 source.remove(j + 1);
65 source.add(j + 1, source.get(j));
66 source.remove(j);
67 source.add(j, tem);
68 }
69 if (source.get(j)[position] == source.get(j + 1)[position]) {
70 if (source.get(j).length > source.get(j + 1).length) {
71 tem = source.get(j + 1).clone();
72 source.remove(j + 1);
73 source.add(j + 1, source.get(j));
74 source.remove(j);
75 source.add(j, tem);
76 }
77 }
78 }
79 }
80 // for(char[] c : source){
81 // System.out.println(c);
82 // }
83 // System.out.println("========================================position="+position);
84 if (position < max) {
85 firstSort(source, position + 1 , max);
86 }
87
88 }
89
90 }
浙公网安备 33010602011771号