1

I need to match the quality of a film and order them. The value can be found in the title

Ex. "The Wolf of Wall Street (2014) (HD, 1080p).mov"

I have a regex that matches the quality:

"\\d\\d\\d[\\d]*p"

For some reason when i compare (compareTo) the values they are ordered:

Ex.

1080p
1080p
480p
480p
480p
720p
720p

480p lists higher than 720p. I believe that this happens because of the p(?). I wonder how I can change my regex to check that there is a p (so the year in the title isn't matched, only the quality) but excludes the p in the matched string.

Tunaki
  • 132,869
  • 46
  • 340
  • 423

3 Answers3

2

Use can use regex "(\\d{3,4})p" to extract number from string with Matcher.group(1) and sort them as numbers, not strings. Strings are compared lexicographically.

Vladimir Petrakovich
  • 4,184
  • 1
  • 30
  • 46
1

You need a special String comparator, which firstly compare by length, then by value:

Comparator<String> LENGTH_FIRST = (s1, s2) -> s1.length() == s2.length() 
                                      ? s1.compareTo(s2) 
                                      : (s1.length() - s2.length());

When sorting using such comparator, longer strings (meaning higher quality) become located after shorter:

List<String> qualities = Arrays.asList("1080p", "480p", "720p");
Collections.sort(qualities, LENGTH_FIRST); // will re-order as [480p, 720p, 1080p]
Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
  • This actually works, but I can't really understand why? –  Mar 19 '16 at 19:02
  • @kletan very simple. For positive integers, any number with more digits is bigger than any number having less digits (`1000` > `999`). Among numbers of same count of digits, bigger is number that is bigger lexicographically. – Alex Salauyou Mar 19 '16 at 19:27
0

I haven't used Java that much, but maybe you could use a group inside the regex? I.e.:

"(\\d\\d\\d[\\d]*)p"

And then make a backreference to the group (content inside the parentheses). I know you can do that with perl regex

Hope it helped!

JuanKman94
  • 112
  • 1
  • 7