简体   繁体   中英

How do i sort a 2d String array in ascending order?

I have a question how do you sort this array in ascending order. I know how to do a single array but, I don't know the syntax for an array like this or how to go about doing this problem. Here is the array.

 String[][] store = {       
        {"Alumni Drink ware", "Gifts", "$25.00"},
        {"Binders", "School Supplies", "$3.00"},
        {"Book bag", "School Supplies", "$20.00"},
        {"Fabulous Desserts", "Textbooks", "$25.00"},
        {"Folders", "School Supplies", "$1.00"},
        {"Gift Cards", "Gifts", "$25.00"},
        {"Highlighters", "School Supplies", "$2.00"},
        {"Jacket", "Campus Gear", "$65.00"},
        {"JAVA Programming", "Textbooks", "$150.00"},
        {"Network Solutions", "Textbooks", "$75.00"},
        {"Pencils", "School Supplies", "$1.00"},
        {"Pens", "School Supplies", "$2.00"},
        {"Shorts", "Campus Gear", "$10.00"},
        {"Sweatshirts", "Campus Gear", "$40.00"},
        {"T-shirts", "campus Gear", "$15.00"},
        {"Web Design Ideas", "Textbooks", "$55.00"}};

I'm trying to sort the array looking at the last column or do you just go through the whole array. I'm completely lost here and would appreciate the help.

The Comparator class allows you code a sorting algorithm. You can then incorporate that Comparator class algorithm in classes like Arrays.sort(...) that take it as a parameter.

Arrays.sort(store,new PriceComparator());

where PriceComparator is a new class like:

public class PriceComparator implements Comparator<String[]>{
    public int compare(String[] arg1, String[] arg2){
        String cost1 = arg1[2].substring(1);
        String cost2 = arg2[2].substring(1);

        BigDecimal value1 = new BigDecimal(cost1);
        BigDecimal value2 = new BigDecimal(cost2);

        return value1.compareTo(value2);
    }
}

If you wanted to sort by the item name, you could create another Comparator and sort it with that. For example:

Arrays.sort(store,new ItemNameComparator());

public class ItemNameComparator implements Comparator<String[]>{
    public int compare(String[] arg1, String[] arg2){
`enter code here`
    }
}

Check out Column Comparator . It shows how to create a simple Comparator to sort on the specific column:

public class Column0Comparator implements Comparator<String[]>
{
    public int compare(String[] array, String[] anotherArray)
    {
        return array[0].compareTo( anotherArray[0] );
    }
};

It also provide a general purpose Comparator that you can reuse on any column in an Array or List.

Here is a brute force method: Let us assume 1 element is one complete row (ie three columns in a row). You have 10 elements ie 10 such type of rows and you want to perform sorting.

Use any algorithm like bubble sort, insertion sort, selection sort, etc to sort these 10 elements. And, to compare 2 elements, perform comparison on the basis of three column in a row. ie

if(arr[0][0] > arr[1][0] )
   {
        // perform swaping of entire row 
    }
   else
    {
       if(arr[0].[2] > arr[1][2])
         {
             //perform swaping of entire row
         }
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM