Be a Supporter!

compareTo & sort [Java]

  • 467 Views
  • 2 Replies
New Topic Respond to this Topic
gumOnShoe
gumOnShoe
  • Member since: May. 29, 2004
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
compareTo & sort [Java] 2009-10-06 17:04:05 Reply

So 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)


Newgrounds Anthology? 20,000 Word Max. [Submit]

Music? Click Sig:

BBS Signature
Jon-86
Jon-86
  • Member since: Jan. 30, 2007
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to compareTo & sort [Java] 2009-10-06 18:10:22 Reply

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!


PHP Main :: C++ Main :: Java Main :: Vorsprung durch Technik
irc.freenode.net #ngprogramming

BBS Signature
fourthfrench
fourthfrench
  • Member since: Aug. 13, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to compareTo & sort [Java] 2009-10-06 18:50:14 Reply

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