0

I am trying to fix my sort method. I am reading in from a txt file and trying to get them in alphabetic order.

I can't use the implements comparable then use collections.sort() because my instructor won't allow it.

//How I am trying to sort
public static void sortPlayers(ArrayList<BingoPlayer> players){
    BingoPlayer temp;
    int value = 0;
    for(int i = 0; i < players.size()-1; i++)
    {
        value = compareTo(players.get(i), players.get(i + 1));

        if(value < 0 || value == 0){
            //continue;
        }
        else if(value > 0){
            temp = players.get(i);
            players.set(i, players.get(i + 1));
            players.set(i + 1, temp);
        }
    }

}
 //How am I comparing. 
public static int compareTo(BingoPlayer player1, BingoPlayer player2){
    if(player1.firstName.compareTo(player2.firstName) < 0)
        return player1.firstName.compareTo((player2.firstName));
    else if(player1.firstName.compareTo(player2.firstName) > 0)
        return player1.firstName.compareTo((player2.firstName));
    else 
        if (player1.lastName.compareTo(player2.lastName) < 0)
            return player1.lastName.compareTo((player2.lastName));
        else if (player1.lastName.compareTo(player2.lastName) > 0)
            return player1.lastName.compareTo((player2.lastName));
            else 
                return 0;
}

Here is the txt file I am reading from:

50.00
10
James,Smith,50.0
Michael,Smith,50.0
Robert,Smith,50.0
Maria,Garcia,50.0
David,Smith,50.0
Maria,Rodriguez,50.0
Mary,Smith,50.0
Maria,Hernandez,50.0
Maria,Martinez,50.0
James,Johnson,50.0
Bikash Das
  • 147
  • 1
  • 12
harvey2011
  • 39
  • 4
  • Try this: https://stackoverflow.com/questions/708698/how-can-i-sort-a-list-alphabetically – Dr4ke the b4dass Apr 03 '19 at 02:34
  • 1
    You didn't specify clearly to fix what. What is the problem here better if you specify. – Bikash Das Apr 03 '19 at 02:36
  • When I use my current sorting method it wont list them in alphabetical order. For instance I need James smith first, then James Johnson, etc... – harvey2011 Apr 03 '19 at 02:39
  • Either use a sorting method from the standard library (recommended) or implement a recognized sorting algorithm from a text book. If you tried the latter, you’ve done it incorrectly, and you may want to tell us which algorithm it should have been. :-) – Ole V.V. Apr 03 '19 at 02:46
  • *Johnson* comes before *Smith* in my alphabet. – Ole V.V. Apr 03 '19 at 02:47
  • 2
    Sounds like you've been asked to write your own sort function, so the link above won't help. For the kind of sort it looks like you're starting to write, you need two loops. As @OleV.V. said, what you want to start with is the idea of what kind of sorting technique you're implementing. I expect that your instructor gave you some direction here. Once you know the technique, then you can work to turn that into code. You might reason it out for yourself, but I'm guessing that your instructor isn't expecting that. – CryptoFool Apr 03 '19 at 02:53
  • Here's a resource that explains how to write a "merge sort". It includes Java code: https://www.geeksforgeeks.org/merge-sort/ - They explain the [Quick Sort](https://www.geeksforgeeks.org/quick-sort/) as well. there are probably many resources like this on the web, teaching the basics of a variety of sorting algorithms. – CryptoFool Apr 03 '19 at 02:56
  • Would be immensely helpful if you pasted your full codes here – allkenang Apr 03 '19 at 03:06

2 Answers2

1

Not allowed for you, but for anyone reading along: Sorting is built into Java, you don’t need to implement it yourself.

    Collections.sort(players,
            Comparator.comparing(BingoPlayer::getFirstName)
                    .thenComparing(BingoPlayer.getLastName));

This will require you to implement getters in your bingo player class, which will be good style anyway. PS I haven’t tested, so forgive if there’s a typo.

Impementing your own sorting

You are taking one pass through the list swapping adjacent players if they are in the wrong order. This will put Maria Garcia before Robert Smith since they were already adjacent, but it will not fix all cases of players that are farther apart in the list. For example, James Johnson will not be moved up to the other players that start with J.

What you are trying to do reminds of the algorithm known as bubble sort, but to work as bubble sort you need to repeat the passes over the list until no more swapping is being done.

PS On method naming: Usually an instance method (non-static method) in the bingo player class comparing this bingo player to another one would be called compareTo, while a method taking two players as arguments and comparing them would be called just compare. So using the former name for a method of the latter kind may confuse some.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

you need to use a nested for loop for sorting

public static void sortPlayers(ArrayList<BingoPlayer> players){
    BingoPlayer temp;
    int value = 0;
    for(int i = 1; i <= players.size()-1; i++)
    {

        for(int j=i;j>0;j--){
            value = compareTo(players.get(j), players.get(j - 1));

            if(value < 0){
                temp = players.get(j);
                players.set(j, players.get(j - 1));
                players.set(j - 1, temp);
            }
        }
    }

}

Also i optimized your comparing function a bit:

public static int compareTo(BingoPlayer player1, BingoPlayer player2){
    if(player1.firstName.compareTo(player2.firstName) != 0)
        return player1.firstName.compareTo((player2.firstName));
    else 
        return player1.lastName.compareTo((player2.lastName));
}
Mustahsan
  • 3,852
  • 1
  • 18
  • 34