-1

Possible Duplicate:
Best way to sort 2 array lists?

package bubblesort;

import java.util.*;

public class BubbleSort {

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    Scanner input2 = new Scanner(System.in);
    List<String> lastnamesList = new ArrayList();
    List<String> firstnamesList = new ArrayList();
    List<Integer> ageList = new ArrayList();       
    List<String> fullnamesList = new ArrayList();
    int decider;
    String EOF = "EOF";


    System.out.println("Enter in 0 to sort by Last Name, Enter in 1 to sort by Age");
    decider = input.nextInt();

    /*if(decider == 0)
    {
    System.out.println("Sorting by Last Name");
        for(int i=0;i<3;i++)
        {
        System.out.println("Please Enter in your Last Name, FirstName, Age: " );
        fullnamesList.add(input2.nextLine());
        }
    Collections.sort(fullnamesList);
    System.out.println("Sorting by Last Name: ");
    System.out.println(fullnamesList);
    }*/ 
    //String[] lastnamesArray = lastnamesList.toArray(new String[lastnamesList.size()]);
    //String[] firstnamesArray = firstnamesList.toArray(new String[firstnamesList.size()]);
    if(decider == 1)
    {
    System.out.println("Sorting by Age");
        for(int i=0;i<3;i++)
        {
        System.out.println("Please Enter in your Last Name: " );
        lastnamesList.add(input2.nextLine());
        System.out.println("Please Enter in your First Name: " );
        firstnamesList.add(input2.nextLine());
        System.out.println("Please Enter in your Age: " );
        ageList.add(input.nextInt());

        }

    String[] firstnamesArray = firstnamesList.toArray(new String[firstnamesList.size()]);
    String[] lastnamesArray = lastnamesList.toArray(new String[lastnamesList.size()]);
    fullnamesList.addAll(firstnamesList);
    fullnamesList.addAll(lastnamesList);
    String[] fullnamesArray = fullnamesList.toArray(new String[firstnamesList.size()+lastnamesList.size()]);

    Collections.sort(fullnamesList);
    System.out.println("Sorting by Age");
    System.out.println(fullnamesList);
    }
}
public static void sortStringBubble(String x[])
{
    int j;
    boolean flag = true;
    String temp;

    while(flag)
    {
        flag = false;
        for(j = 0;j < x.length -1 ; j++)
        {
            if (x [j].compareToIgnoreCase(x[j+1])>0)
            {
                temp = x[j];
                x[j] = x[j+1];
                x[j+1] = temp;
                flag = true;
            }
        }
    }
}

}

im trying to keep the first name, last name, and age together, but i want it to be sorted alphabetically as one instead of sorting them separately.

Community
  • 1
  • 1
Dyl Lao
  • 1
  • 1
  • 1
  • Not a duplicate. The answer shows OP's approach is wrong and only one ArrayList is needed. – fredt Dec 09 '12 at 23:58

1 Answers1

2

Instead of

List<String> lastnamesList = new ArrayList();
List<String> firstnamesList = new ArrayList();
List<Integer> ageList = new ArrayList();   

use

List<Person> personList = new ArrayList<>();

where Person is a class you write yourself. Than there is only one list to sort.

Person would have the fields lastName, firstName and age, so they'll always be together.

Also, see this and this post, or some of these.

Community
  • 1
  • 1
jlordo
  • 37,490
  • 6
  • 58
  • 83
  • I'm fairly new to java so the person class is quite confusing for me, are there any algorithms that can just swap the positions of the name and age according to alphabetical order and keep the fields together – Dyl Lao Dec 09 '12 at 19:13
  • There's no reasonable alternative to the `Person` class, as there are no algorithms for sorting multiple lists according to the index changes of one list. – jlordo Dec 09 '12 at 19:22
  • You need a Comparator for the Person class. – fredt Dec 10 '12 at 00:01