|
| 1 | +package programmers.level2.week_23; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.List; |
| 7 | +import java.util.regex.Pattern; |
| 8 | + |
| 9 | +/** |
| 10 | + * [3차] 파일명 정렬 |
| 11 | + * https://programmers.co.kr/learn/courses/30/lessons/17686?language=java |
| 12 | + */ |
| 13 | +public class Solution003 { |
| 14 | + public static void main(String[] args) { |
| 15 | + Solution003 sol = new Solution003(); |
| 16 | + String[] files = { "img12.png", "img10.png", "img02.png", "img1.png", "IMG01.GIF", "img2.JPG" }; |
| 17 | + System.out.println(Arrays.toString(sol.solution(files))); |
| 18 | + } |
| 19 | + |
| 20 | + class FileObject implements Comparable<FileObject> { |
| 21 | + String head; |
| 22 | + int number; |
| 23 | + String tail; |
| 24 | + int origIdx; |
| 25 | + String origTxt; |
| 26 | + |
| 27 | + public FileObject() { |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + public FileObject(String head, int number, String tail, int origIdX, String origTxt) { |
| 32 | + this.head = head; |
| 33 | + this.number = number; |
| 34 | + this.tail = tail; |
| 35 | + this.origIdx = origIdX; |
| 36 | + this.origTxt = origTxt; |
| 37 | + } |
| 38 | + |
| 39 | + public String getHead() { |
| 40 | + return head; |
| 41 | + } |
| 42 | + |
| 43 | + public void setHead(String head) { |
| 44 | + this.head = head; |
| 45 | + } |
| 46 | + |
| 47 | + public int getNumber() { |
| 48 | + return number; |
| 49 | + } |
| 50 | + |
| 51 | + public void setNumber(int number) { |
| 52 | + this.number = number; |
| 53 | + } |
| 54 | + |
| 55 | + public String getTail() { |
| 56 | + return tail; |
| 57 | + } |
| 58 | + |
| 59 | + public void setTail(String tail) { |
| 60 | + this.tail = tail; |
| 61 | + } |
| 62 | + |
| 63 | + public int getOrigIdx() { |
| 64 | + return origIdx; |
| 65 | + } |
| 66 | + |
| 67 | + public void setOrigIdx(int origIdx) { |
| 68 | + this.origIdx = origIdx; |
| 69 | + } |
| 70 | + |
| 71 | + public String getOrigTxt() { |
| 72 | + return origTxt; |
| 73 | + } |
| 74 | + |
| 75 | + public void setOrigTxt(String origTxt) { |
| 76 | + this.origTxt = origTxt; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public int compareTo(FileObject o) { |
| 81 | + int comH = o.getHead().compareTo(getHead()); |
| 82 | + boolean comN = o.getNumber() < getNumber(); |
| 83 | + boolean comO = o.getOrigIdx() < getOrigIdx(); |
| 84 | + if (comH < 0) { |
| 85 | + return 1; |
| 86 | + } else { |
| 87 | + if (comH == 0) { |
| 88 | + if (comN == true) { |
| 89 | + return 1; |
| 90 | + } else { |
| 91 | + if (comH == 0 && o.getNumber() == getNumber() && comO == true) { |
| 92 | + return 1; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + return -1; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public String[] solution(String[] files) { |
| 102 | + String[] answer = new String[files.length]; |
| 103 | + List<FileObject> list = new ArrayList<>(); |
| 104 | + |
| 105 | + for (int k = 0; k < files.length; k++) { |
| 106 | + String f = files[k].toLowerCase(); |
| 107 | + String head = ""; |
| 108 | + int number = 0; |
| 109 | + String tail = ""; |
| 110 | + for (int i = 0; i < f.length(); i++) { |
| 111 | + if (Pattern.matches("[0-9]", f.charAt(i) + "")) { |
| 112 | + head = f.substring(0, i); |
| 113 | + int stopIdx = 0; |
| 114 | + for (int j = i + 1; j < f.length(); j++) { |
| 115 | + if (Pattern.matches("[0-9]", f.charAt(j) + "") == false) { |
| 116 | + stopIdx = j; |
| 117 | + break; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (stopIdx == 0) { |
| 122 | + number = Integer.parseInt(f.substring(i)); |
| 123 | + } else { |
| 124 | + number = Integer.parseInt(f.substring(i, stopIdx)); |
| 125 | + tail = f.substring(stopIdx); |
| 126 | + } |
| 127 | + |
| 128 | + list.add(new FileObject(head, number, tail, k, files[k])); |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + Collections.sort(list); |
| 135 | + |
| 136 | + for (int i = 0; i < list.size(); i++) { |
| 137 | + answer[i] = list.get(i).getOrigTxt(); |
| 138 | + } |
| 139 | + |
| 140 | + return answer; |
| 141 | + } |
| 142 | +} |
0 commit comments