Created
January 27, 2017 14:26
-
-
Save DominicFinn/fc0615f38cf68995667c43b8c1e18a9d to your computer and use it in GitHub Desktop.
Java orderizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.List; | |
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
/** | |
* | |
* @author dominicfinn | |
*/ | |
public class Sorter { | |
private List<String> values; | |
public Sorter() { | |
values = new ArrayList<>(); | |
values.add("American Apparell"); | |
values.add("Apple"); | |
values.add("Apful"); | |
values.add("Gap"); | |
values.add("Miss Papp"); | |
values.add("Paper Chase"); | |
} | |
public List<String> Sort(String search) { | |
List<String> contains = new ArrayList<>(); | |
List<String> startsWith = new ArrayList<>(); | |
for(String item : values) { | |
if (item.toLowerCase().contains(search.toLowerCase())) { | |
if(item.toLowerCase().startsWith(search.toLowerCase())) { | |
startsWith.add(item); | |
} else { | |
contains.add(item); | |
} | |
} | |
} | |
Collections.sort(contains, ALPHABETICAL_ORDER); | |
Collections.sort(startsWith, ALPHABETICAL_ORDER); | |
startsWith.addAll(contains); | |
return startsWith; | |
} | |
private static Comparator<String> ALPHABETICAL_ORDER = new Comparator<String>() { | |
public int compare(String str1, String str2) { | |
int res = String.CASE_INSENSITIVE_ORDER.compare(str1, str2); | |
if (res == 0) { | |
res = str1.compareTo(str2); | |
} | |
return res; | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
import java.util.HashSet; | |
import java.util.List; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
/** | |
* | |
* @author dominicfinn | |
*/ | |
public class SortingTests { | |
private Sorter subject; | |
@Before | |
public void setUp() { | |
this.subject = new Sorter(); | |
} | |
@Test | |
public void hello() { | |
Object[] results; | |
results = subject.Sort("ap").toArray(); | |
Assert.assertEquals("Apful", (String)results[0]); | |
Assert.assertEquals("Apple", (String)results[1]); | |
Assert.assertEquals("American Apparell", (String)results[2]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment