Created
November 11, 2012 22:33
-
-
Save tomwhoiscontrary/4056538 to your computer and use it in GitHub Desktop.
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.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Random; | |
public class Sorter { | |
public static void main(String[] args) { | |
@SuppressWarnings("unchecked") | |
IndexEntry<String>[] a = new IndexEntry[] { | |
new IndexEntry<String>("index.html", 6, 2), | |
new IndexEntry<String>("chickadee.html", 3, 2), | |
new IndexEntry<String>("cuckoo.html", 3, 2), | |
new IndexEntry<String>("crow.html", 3, 2), | |
new IndexEntry<String>("crane.html", 3, 2), | |
new IndexEntry<String>("cardinal.html", 3, 2), | |
new IndexEntry<String>("quail.html", 2, 2), | |
new IndexEntry<String>("robin.html", 2, 2), | |
new IndexEntry<String>("finch.html", 2, 2), | |
new IndexEntry<String>("kingfisher.html", 2, 2), | |
new IndexEntry<String>("swan.html", 2, 2), | |
new IndexEntry<String>("turkey.html", 2, 2), | |
new IndexEntry<String>("ibis.html", 2, 2), | |
new IndexEntry<String>("falcon.html", 2, 2), | |
new IndexEntry<String>("albatross.html", 2, 2), | |
new IndexEntry<String>("tern.html", 2, 2), | |
new IndexEntry<String>("dove.html", 2, 2), | |
new IndexEntry<String>("mockingbird.html", 2, 2), | |
new IndexEntry<String>("owl.html", 2, 2), | |
new IndexEntry<String>("sparrow.html", 2, 2), | |
new IndexEntry<String>("hawk.html", 2, 2), | |
new IndexEntry<String>("magpie.html", 2, 2), | |
new IndexEntry<String>("nighthawk.html", 2, 2), | |
new IndexEntry<String>("gull.html", 2, 2), | |
new IndexEntry<String>("bluebird.html", 2, 2), | |
new IndexEntry<String>("raven.html", 2, 2), | |
new IndexEntry<String>("blackbird.html", 2, 2), | |
new IndexEntry<String>("sandpiper.html", 2, 2), | |
new IndexEntry<String>("pheasant.html", 2, 2), | |
new IndexEntry<String>("roadrunner.html", 2, 2), | |
new IndexEntry<String>("duck.html", 2, 2), | |
new IndexEntry<String>("vulture.html", 2, 2), | |
new IndexEntry<String>("pigeon.html", 2, 2) }; | |
List<? extends IndexEntry<String>> matches = Arrays.asList(a); | |
Collections.shuffle(matches, new Random("Tulse Luper".hashCode())); | |
//Get a list of the values | |
ArrayList<IndexEntry<String>> cleanEntries = new ArrayList<IndexEntry<String>>(matches); | |
//Sort Files/URLS | |
Collections.sort(cleanEntries); | |
for (IndexEntry<String> indexEntry : cleanEntries) { | |
System.out.println(indexEntry); | |
} | |
} | |
static class IndexEntry<T> implements Comparable<IndexEntry<T>> { | |
private final String locationName; | |
private final int occurrenceCount; | |
private final int firstOccurrence; | |
public IndexEntry(String locationName, int occurrenceCount, int firstOccurrence) { | |
this.occurrenceCount = occurrenceCount; | |
this.firstOccurrence = firstOccurrence; | |
this.locationName = locationName; | |
} | |
public int getOccurrenceCount() { | |
return occurrenceCount; | |
} | |
public int getFirstOccurrence() { | |
return firstOccurrence; | |
} | |
public String getLocationName() { | |
return locationName; | |
} | |
@Override | |
public String toString() { | |
return "\"" + locationName + "\", " + occurrenceCount + ", " + firstOccurrence; | |
} | |
@Override | |
public int compareTo(IndexEntry<T> other) { | |
if (this.occurrenceCount < other.getOccurrenceCount()) { | |
return 1; | |
} | |
else if (this.occurrenceCount > other.getOccurrenceCount()) { | |
return -1; | |
} | |
else {//Need to compare Initial Positions | |
if (this.firstOccurrence == other.getFirstOccurrence()) { | |
return this.getLocationName().compareTo(other.getLocationName()); | |
} | |
else if (this.firstOccurrence > other.getFirstOccurrence()) { | |
return 1; | |
} | |
else { | |
return -1; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment