The Enchanted Cave 2
Delve into a strange cave with a seemingly endless supply of treasure, strategically choos
4.34 / 5.00 31,296 ViewsGhostbusters B.I.P.
COMPLETE edition of the interactive "choose next panel" comic
4.07 / 5.00 10,082 ViewsSo I created a Row class which just holds onto an ArrayList of Strings.
Each Row has a set number of unique identifiers. That is if there are 30 Rows in a "Document" then indices 0, 1, and 2 could be unique (primary keys), so that when I sort the document I want it sorted by the first three strings in each array list, 0 being the most important, then 1 and 2 in that order.
Java's built in merge sort is stable, so it seems if I could sort by 2 and then 1 and then 0, I'd be golden, but Java's built in sort uses the compareTo method to sort a given list. [See Collection in the JavaDocs]
Strings also have a built in compareTo method, but returning the value of one string doesn't cut it as that value may be compared to a different index later on by Collection.sort(). Similarly, I can't simply concatenate the strings because if String A = "ab" and String B = "a", then String C = "a" and String D "ba" would result in Row { A, B } == Row { C, D }.
I'm curios as to what you think the solution to this is... I'm sure its something simple I haven't thought of... also speed is essential. (I could be dealing with a million rows easily)
Are you trying to sort alphabetically? Word length? Word frequency? The way I read it you have explained what your doing and not the problem your trying to solve. Unless I read it wrong. Let me know and I should be able to get back to you the morra. Need to sleep now.
Unless you or someone else figures it out before then, sounds like an interesting problem!
I'm a little confused by your question, but is this what you are looking to do?
import java.util.ArrayList;
import java.util.Collections;
class Row implements Comparable<Row>
{
ArrayList<String> al;
public Row(ArrayList<String> al){this.al = al;}
public int compareTo(Row o)
{
for(int i=0; i<3; i++)
if(!al.get(i).equals(o.al.get(i)))
return al.get(i).compareTo(o.al.get(i));
return 0;
}
public String toString(){return al.toString();}
}
public class Sort
{
public static void main(String[] args)
{
ArrayList<String> one = new ArrayList<String>();
ArrayList<String> two = new ArrayList<String>();
one.add("ab");
one.add("a");
two.add("a");
two.add("ba");
Row a = new Row(one);
Row b = new Row(two);
ArrayList<Row> rows = new ArrayList<Row>();
rows.add(a);
rows.add(b);
Collections.sort(rows);
System.out.println(rows);
//[[a, ba], [ab, a]]
}
}
~fourthfrench