2019년 4월 10일 수요일

[Java/Python] String and Number mixed text sorting


1. java

1
 2
 3
 4
 5
 6
 7
 8
 9
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;

import net.lingala.zip4j.model.FileHeader;

public class SortUtil {
 
 public static int compareStringNumber(String s1, String s2) {
  //String[] s1Parts = s1.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
  //String[] s2Parts = s2.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
  String[] s1Parts = s1.split("(\\d+)");
  String[] s2Parts = s2.split("(\\d+)");
  int i = 0;
  while(i < s1Parts.length && i < s2Parts.length){
      if(s1Parts[i].compareTo(s2Parts[i]) == 0){ //if parts are the same
       ++i;
   }else{
       try{
           int intS1 = Integer.parseInt(s1Parts[i]);
           int intS2 = Integer.parseInt(s2Parts[i]);
              int diff = intS1 - intS2; 
              if(diff == 0){
                  ++i;
              }else{
                  return diff;
              }
          }catch(Exception ex){
              return s1.compareTo(s2);
          }
      }
  }
  if(s1.length() < s2.length()){
      return -1;
  }else if(s1.length() > s2.length()){
      return 1;
  }else{
      return 0;
  }
 }  

 public static void sortStringNumber(List<String> strings) {
     Collections.sort(strings, new Comparator<String>() {
      public int compare(String s1, String s2) {
       return compareStringNumber(s1, s2);
      }
     });
 }
 
 public static void sortFiles(List<File> strings) {
     Collections.sort(strings, new Comparator<File>() {
      public int compare(File s1, File s2) {
       return compareStringNumber(s1.getPath(), s2.getPath());
      }
     });
 }
 
 public static void sortZipFileHeader(List<FileHeader> strings) {
     Collections.sort(strings, new Comparator<FileHeader>() {
      public int compare(FileHeader f1, FileHeader f2) {
       return compareStringNumber(f1.getFileName(), f2.getFileName());
      }
     });
 }
 
 public static void sortZipEntry(List<ZipArchiveEntry> strings) {
     Collections.sort(strings, new Comparator<ZipArchiveEntry>() {
      public int compare(ZipArchiveEntry f1, ZipArchiveEntry f2) {
       return compareStringNumber(f1.getName(), f2.getName());
      }
     });
 }
 
 public static void main(String[] args) throws IOException {
     List<String> strings = Arrays.asList("room2/1.jpg", "room2/2.jpg","room1/5.jpg", "foo", "zoo", "room2", "room100", "room10/5.jpg");
     SortUtil.sortStringNumber(strings);
     System.out.println(strings);
 }
}



2. Python

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def keyStrNumSort(strItem):
    import re
    strList = re.split('(\d+)',strItem)
    strList = [x for x in strList if len(x) > 0]
    newList = []
    for s in strList:
        try:
            newList.append(int(s))
        except:
            newList.append(s)            
    return newList

def TestStrNumSort():
    strList = [
        'D:/fold_01/book_001',
        'D:/fold_01/book_010',
        'D:/fold_1/book_2',
        'D:/fold_1/book_100',
        'D:/02/book_100',
        'D:/fold_02/book_010',
        'D:/fold_02/book_2',
        'D:/fold_3/book_020',
        'D:/fold_04/book_001',
    ]
    print(strList)
    strList2 = sorted(strList)
    keyStrNumSort(strList2[0])
    print(sorted(strList))
    print(sorted(strList,key=keyStrNumSort))
    
if __name__ == '__main__':
    TestStrNumSort()



댓글 없음:

댓글 쓰기