Question

Sort an ArrayList<String[]> by name in Java The ArrayList<String[]> is inserted as follows Apple NY A1...

Sort an ArrayList<String[]> by name in Java

The ArrayList<String[]> is inserted as follows

Apple NY A1
Carrots SF C2
Banana SEA B3

Sort the first column so

Apple NY A1
Banana SEA B3
Carrots SF C2

Homework Answers

Answer #1

/******* ArrayListStringArrayDemo.java ******/

import java.util.ArrayList;

public class ArrayListStringArrayDemo {

   public static void main(String[] args) {

       // Creating ArrayList Which holds String Arrays
       ArrayList<String[]> arl = new ArrayList<String[]>();
       String s1[] = { "Apple", "Carrots", "Banana" };

       String s2[] = { "NY", "SF", "SEA" };
      
       // Adding arrays to the ArrayList
       arl.add(s1);
       arl.add(s2);

       String temp;
      
       // Displaying the arrays in the arraylist
       System.out.println("_______ Before Sorting ______");
       for (int i = 0; i < arl.get(0).length; i++) {
           System.out.printf("%-30s%s\n", arl.get(0)[i], arl.get(1)[i]);
       }

       // This Logic will Sort the ArrayList of Arrays of elements in Ascending
       // order
       for (int h = 0; h < arl.size(); h++) {

           for (int i = 0; i < arl.get(h).length; i++) {
               for (int j = i + 1; j < arl.get(h).length; j++) {
                   if (arl.get(h)[i].compareTo(arl.get(h)[j]) > 0) {
                       temp = arl.get(h)[i];
                       arl.get(h)[i] = arl.get(h)[j];
                       arl.get(h)[j] = temp;
                   }
               }
           }
       }

       // Displaying the arrays in the arraylist
       System.out.println("_______ After Sorting ______");
       for (int i = 0; i < arl.get(0).length; i++) {
           System.out.printf("%-30s%s\n", arl.get(0)[i], arl.get(1)[i]);
       }

   }

}

/****************************************************/

/****************************************************/

Output:

/****************************************************/

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the code to be written with these) I need Class river, Class CTRiver and Class Driver with comments so I can learn and better understand the code I also need a UML Diagram HELP Please! Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong()...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs in Chapter 1 of your textbook are not complete. They are used for illustration purpose only. The implementation of Listing 1-1 on page 39 is explained in Chapter 2. And, in order to see the result of using it, we will need the following set of files: i. BagInteface.java – the specification only. ii. ArrayBag.java – the implementation of BagInerface.java. iii. ArrayBagDemo.java – a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT