Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #33424

Re: Java sort

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Java sort
Date 2015-12-28 22:47 -0500
Organization Aioe.org NNTP Server
Message-ID <n5svo0$vg0$1@speranza.aioe.org> (permalink)
References <ab12b5b7-eab1-4f4a-93c1-3c386d64a7f3@googlegroups.com>

Show all headers | View raw


On 12/28/2015 3:08 PM, Eric Douglas wrote:
> My program called java.io.File.listFiles(), then java.util.Collections.sort().
> My results:
> 1.pdf
> 10.pdf
> 100.pdf
> 2.pdf
> 3.pdf
> Windows Explorer's results:
> 1.pdf
> 2.pdf
> 3.pdf
> 10.pdf
> 100.pdf
>
> I asked Google and it says Windows Explorer uses this:
> https://msdn.microsoft.com/en-us/library/bb759947(VS.85).aspx
> I asked Google for a Java equivalent and I'm not finding an easy answer.  Is there one I'm missing?  I hate to reinvent a wheel.

I think you will have to write some custom code.

The key point must be to isolate the file name to numeric value logic.

One suggestion:

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SpecialSort {
	public static interface Evaluator {
		public int value(String s);
	}
	public static class StartingInteger implements Evaluator {
		private static final Pattern p = Pattern.compile("\\d+");
		@Override
		public int value(String s) {
			Matcher m = p.matcher(s);
			if(m.find()) {
				return Integer.parseInt(m.group());
			} else {
				return 0;
			}
		}
	}
	public static class ValueComparator implements Comparator<String> {
		private Evaluator eval;
		public ValueComparator(Evaluator eval) {
			this.eval = eval;
		}
		@Override
		public int compare(String s1, String s2) {
			return eval.value(s1) - eval.value(s2);
		}
	}
	public static void main(String[] args) {
		List<String> lst = Arrays.asList("1.pdf", "10.pdf", "100.pdf", 
"2.pdf", "3.pdf" );
		System.out.println(lst);
		Collections.sort(lst);
		System.out.println(lst);
		Collections.sort(lst, new ValueComparator(new StartingInteger()));
		System.out.println(lst);
	}
}

Arne

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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