0

In android, how do you sort multiple values in a single row based on priority, similar to the Sort function in Excel?

Map<String,String,Float,Integer> shoes; // Style, Color, Price, Quantity
shoes.put("Boots","Red",15.50,5);
shoes.put("Boots","Green",17.50,2);
shoes.put("Skate","Red",12.25,6);
shoes.put("Skate","Blue",13.05,9);
shoes.Sort(1,0,2,3); // sorts by color, then by style

shoes.Sort(2,0,1,3); // sorts by price, then by style
Kae Cyphet
  • 185
  • 2
  • 6
  • 2
    Are you SURE you don't want to make a Shoes object and use [Comparators](http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html)? – Thomas Oct 11 '12 at 21:34
  • Im open to all suggestions. Any code samples would help greatly. – Kae Cyphet Oct 11 '12 at 21:37

2 Answers2

0

The suggestion made by Kae is correct, but, you can do something like this:

  1. Create a class (internal or not) that implements the interface Comparator and it should as the infer type related what you want to compare.
  2. Create a new TreeSet with the same specification of the Map and with the Comparator as parameter
  3. Put all objects into the created treeseet.

Look this thread: Sort a Map<Key, Value> by values (Java)

But again, you should think about creating an Class to your need.

[]s Neto

Community
  • 1
  • 1
Neto Marin
  • 674
  • 3
  • 15
0

There's a good block of example code here that covers setting up a general comparator. To sort on multiple columns will take multiple calls, but it will have the desired effect. From the Collections spec

This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.

So you start with a Shoe

public class Shoe
{
  String style;
  String color;
  float price;//There are some reasons to not use float for money, google has lots of blogs etc
  int quantity;
}

Then make your comparators based on the tutorial linked above, and eventually:

ArrayList<Shoe> shoes = makeShoes();
Collections.sort(shoes,col1Comparator);
Collections.sort(shoes,col2Comparator);
Collections.sort(shoes,col3Comparator);
Thomas
  • 5,074
  • 1
  • 16
  • 12
  • Thats a nice link, but it seems to do this simple task will take more than 50 lines of code. – Kae Cyphet Oct 12 '12 at 00:37
  • It's a solution designed to handle potentially complex situations. Your particular goal is one of the simplest, which is why it matches the linked tutorial well but looks like overkill. The same structure could be used to sort say, a list of outfits(which have shoes) by shoe color. Or sort things that don't have a default ordering, like sorting people by happiness as defined in the comparator. If your problem is better solved with a spreadsheet/database, you should consider those as alternatives. – Thomas Oct 12 '12 at 04:28
  • I find it simpler to CSV the values into a 1D array, sort the array, then explode the values. Was hoping for a sleek solution, but alas, Java, so many souls belong to thee. – Kae Cyphet Oct 12 '12 at 08:51
  • That seems like it will still easily break the 50 line mark. If you have all the fields in one array, sorting won't mean anything without extra work on your part to maintain the groupings, while if you have one array for each field you still have extra code to make sure that style[1] matches correctly with color[1], price[1], and quality[1]. – Thomas Oct 12 '12 at 12:39