0

I have an ArrayList with a couple of simple user objects in it which contains information such as their ID, name, and password:

user1 = new User(19, "bsmith", "password");
user2 = new User(15, "jbrown", "password");
user3 = new User(23, "sgreen", "password");

userArray.add(person1);
userArray.add(person2);
userArray.add(person3);

I'm trying to sort them by their user ID using this bubble sort method I found:

public static void BubbleSort( int [ ] num )
{
 int j;
 boolean flag = true;   
 int temp;   

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

I just have no idea how to actually use it on my ArrayList. I managed to get it to sort a regular array though:

int j[] = {10,30,20,50};

People p = new People();

p.BubbleSort(j);

for (int i = 0; i < j.length; i++) {
    System.out.println(j[i]);
}

My question: Is it possible to use the bubble sort directly on the ID of each user in my ArrayList, or do I have to somehow put each users's ID into another array and use the bubble sort method on that?

Andy
  • 364
  • 1
  • 8
  • 21

3 Answers3

0

The method you have found can be used on only for arrays of int values: int[], what you have is an ArrayList of User Objects: ArrayList<User>. So you need to do some changes to it in order to use it for your ArrayList.

First the signature, change

public static void BubbleSort( int [ ] num )

to

public static void BubbleSort( ArrayList<User> userList )

Then for accessing single elements you cannot use the index operator, you need to use the get and set methods of the ArrayList.

Sin the method is static, you don't have to create an instance to call it, just use the class name:

People.BubbleSort( userArray );

Finally Note that it is preferable to use an interface for declaring data types. So for ArrayList you could use the List Interface line so:

List<User> userArray = new ArrayList<>();

and

public static void BubbleSort( List<User> userList )

Also please consider Java naming convention for methods and variables, they should always start with lower-case letters.

public static void bubbleSort( List<User> userList )
A4L
  • 17,353
  • 6
  • 49
  • 70
0

This is an example of how you can change your method to sort ArrayList of Integers

public static void BubbleSort( ArrayList<Integer> num )
{
    int j;
    boolean flag = true;
    int temp;

    while ( flag )
    {
        flag= false;
        for( j=0;  j < num.size() - 1;  j++ )
        {
            if ( num.get(j) > num.get(j+1) )
            {
                temp = num.get(j);
                num.set(j, num.get(j+1));
                num.set(j + 1, temp);
                flag = true;
            }
        }
    }
}

Now all you need is to change it to compare by user.age, like

if ( num.get(j).getAge() > num.get(j+1).getAge() )

not sure what is the interface of User class

Belerafon
  • 488
  • 4
  • 13
0

I would recommend you making a getID() method in the user class which returns the String ID.

private String id;
public String getID() {
    return id;
}

Then just call getID and sort the users by sorting the Strings

IntegralOfTan
  • 640
  • 6
  • 18