Skip to content

Instantly share code, notes, and snippets.

@robbiemu
Created April 27, 2016 13:39
Show Gist options
  • Save robbiemu/7d2f3968b825707c4e4d6f3f8121dd81 to your computer and use it in GitHub Desktop.
Save robbiemu/7d2f3968b825707c4e4d6f3f8121dd81 to your computer and use it in GitHub Desktop.
import java.util.*;
import java.lang.*;
/**
* Created by RobertoTomás on 0027, 27, 4, 2016.
*/
public class StringSorter {
public static void main(String[] args) {
//Arrays.stream(args).sorted(Comparator.naturalOrder());
args = new String[] {"1","two","zwei","fourth", "fourz", "fourse"};
args = Arrays.stream(args).sorted((in1,in2) -> {
int len1 = in1.length();
int len2 = in2.length();
int lim = Math.min(len1, len2);
char[] v1 = new char[len1];
char[] v2 = new char[len2];
in1.getChars(0,len1, v1,0);
in2.getChars(0,len2, v2,0);
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
if ((c1 == 't')||(c1 == 'T')) {
return 1;
}
if ((c2 == 't')||(c2 == 'T')) {
return -1;
}
return c1 - c2;
}
k++;
}
return len1 - len2;
}).toArray(size -> new String[size]);
for(String s: args) {
System.out.println(s);
}
}
}
@robbiemu
Copy link
Author

This was an interview question that was asked in pen-and-paper form. I needed to go back afterwards and actually complete the exercise for my own appreciation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment