Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #33425
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Java sort |
| Date | 2015-12-28 22:49 -0500 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <n5svsc$vg0$2@speranza.aioe.org> (permalink) |
| References | <ab12b5b7-eab1-4f4a-93c1-3c386d64a7f3@googlegroups.com> <n5s69i$1i5$1@dont-email.me> <e940a654-c6d0-4c24-b66d-dc55f40936db@googlegroups.com> <e31a1760-2592-44cd-a66d-a10a3330a769@googlegroups.com> <b71feae8-502d-4da9-846b-b235f74925ee@googlegroups.com> |
On 12/28/2015 4:16 PM, Eric Douglas wrote:
> On Monday, December 28, 2015 at 4:02:52 PM UTC-5, Eric Douglas
> wrote:
>> On Monday, December 28, 2015 at 3:49:47 PM UTC-5, Eric Douglas
>> wrote:
>>> On Monday, December 28, 2015 at 3:35:41 PM UTC-5, Eric Sosman
>>> wrote:
>>>> Nothing that comes in Java's standard stuff, as far as I know.
>>>> But you might want
>>>>
>>>> http://sourcefrog.net/projects/natsort/
>>>
>>> Is there Java code in that link? I don't see it. So far I only
>>> found 2 results and they give a different order than Windows, at
>>> least the default order for Windows 8.1.
>>> https://github.com/paour/natorder
>>> http://www.davekoelle.com/files/AlphanumComparator.java
>>>
>>> That first one even includes an example: String[] strings = new
>>> String[] { "1-2", "1-02", "1-20", "10-20", "fred", "jane",
>>> "pic01", "pic2", "pic02", "pic02a", "pic3", "pic4", "pic 4 else",
>>> "pic 5", "pic05", "pic 5", "pic 5 something", "pic 6", "pic 7",
>>> "pic100", "pic100a", "pic120", "pic121", "pic02000", "tom",
>>> "x2-g8", "x2-y7", "x2-y08", "x8-y8" };
>>>
>>> Their result: Sorted: [1-02, 1-2, 1-20, 10-20, fred, jane, pic01,
>>> pic02, pic2, pic02a, pic3, pic4, pic 4 else, pic05, pic 5, pic 5,
>>> pic 5 something, pic 6, pic 7, pic100, pic100a, pic120, pic121,
>>> pic02000, tom, x2-g8, x2-y7, x2-y08, x8-y8] Windows (I wrote
>>> those out as empty files): [1-02, 1-2, 1-20, 10-20, fred, jane,
>>> pic 7, pic 4 else, pic 5, pic 5 something, pic 6, pic01, pic02,
>>> pic02a, pic2, pic3, pic4, pic05, pic100, pic100a, pic120, pic121,
>>> pic02000, tom, x2-g8, x2-y7, x2-y08, x8-y8]
>>
>> I also tried the Comparator in the last post in this thread
>> http://stackoverflow.com/questions/1262239/natural-sort-order-string-comparison-in-java-is-one-built-in
>>
>>
>> I think it's closer, though a couple results are still out of place.
>> It got some right the other one got wrong so it seems they're both
>> half right.
>> Sorted: [1-2, 1-02, 1-20, 10-20, fred, jane, pic 7, pic 4 else,
>> pic 5 something, pic 5, pic 5, pic 6, pic01, pic2, pic02, pic02a,
>> pic3, pic4, pic05, pic100a, pic100, pic120, pic121, pic02000, tom,
>> x2-g8, x2-y7, x2-y08, x8-y8]
>
> The answer in the middle of that thread, by Olivier Oudot, is closer
> - still not sorting numerics with leading zeroes first.
This is a much harder problem than your first post indicated.
But I will suggest something similar.
One attempt:
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class VerySpecialSort {
public static interface Normalizer {
public String convert(String s);
}
public static class WindowsHack implements Normalizer {
private void xappend(StringBuilder sb, StringBuilder tmp) {
if(tmp.length() > 0) {
sb.append(String.format("%010d%c", Integer.parseInt(tmp.toString()),
tmp.charAt(0) == '0' ? 'A' : 'B'));
tmp.delete(0, tmp.length());
}
}
@Override
public String convert(String s) {
StringBuilder sb = new StringBuilder();
StringBuilder tmp = new StringBuilder();
for(char c : s.toCharArray()) {
if(Character.isDigit(c)) {
tmp.append(c);
} else if(c == ' ' || c == '-' || Character.isUpperCase(c)) {
xappend(sb, tmp);
sb.append(c);
} else if(Character.isLowerCase(c)) {
xappend(sb, tmp);
sb.append(Character.toUpperCase(c));
}
}
xappend(sb, tmp);
return sb.toString();
}
}
public static class NormalizerComparator implements Comparator<String> {
private Normalizer norm;
public NormalizerComparator(Normalizer norm) {
this.norm = norm;
}
@Override
public int compare(String s1, String s2) {
return norm.convert(s1).compareTo(norm.convert(s2));
}
}
public static void main(String[] args) {
List<String> lst = Arrays.asList("1-2", "1-02", "1-20", "10-20",
"fred", "jane", "pic01", "pic2", "pic02", "pic02a", "pic3", "pic4", "pic
4 else", "pic 5", "pic05", "pic 5 something", "pic 6", "pic 7",
"pic100", "pic100a", "pic120", "pic121", "pic02000", "tom", "x2-g8",
"x2-y7", "x2-y08", "x8-y8" );
System.out.println(lst);
Collections.sort(lst);
System.out.println(lst);
Collections.sort(lst, new NormalizerComparator(new WindowsHack()));
System.out.println(lst);
List<String> res = Arrays.asList("1-02", "1-2", "1-20", "10-20",
"fred", "jane", "pic 7", "pic 4 else", "pic 5", "pic 5 something",
"pic 6", "pic01", "pic02", "pic02a", "pic2", "pic3", "pic4", "pic05",
"pic100", "pic100a", "pic120", "pic121", "pic02000", "tom", "x2-g8",
"x2-y7", "x2-y08", "x8-y8");
System.out.println(res);
}
}
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Java sort Eric Douglas <e.d.programmer@gmail.com> - 2015-12-28 12:08 -0800
Re: Java sort Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-12-28 15:35 -0500
Re: Java sort Eric Douglas <e.d.programmer@gmail.com> - 2015-12-28 12:49 -0800
Re: Java sort Eric Douglas <e.d.programmer@gmail.com> - 2015-12-28 13:02 -0800
Re: Java sort Eric Douglas <e.d.programmer@gmail.com> - 2015-12-28 13:16 -0800
Re: Java sort Arne Vajhøj <arne@vajhoej.dk> - 2015-12-28 22:49 -0500
Re: Java sort Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-12-28 17:55 -0500
Re: Java sort leif.roar@dimnakorr.com - 2015-12-29 01:20 -0600
Re: Java sort Andreas Leitgeb <avl@logic.at> - 2015-12-28 23:17 +0000
Re: Java sort Eric Douglas <e.d.programmer@gmail.com> - 2015-12-29 05:41 -0800
Re: Java sort Arne Vajhøj <arne@vajhoej.dk> - 2015-12-28 22:47 -0500
Re: Java sort Jeff Higgins <jeff@invalid.invalid> - 2015-12-29 15:03 +0000
csiph-web