user4348339
user4348339

Reputation:

Sorting specific column in java?

System.out.println("Please choose an option to forsort the following stats");
System.out.println("A.Player's j=0;Name");
System.out.println("B.Player's FG%");
System.out.println("C.Player's 3P%");
System.out.println("D.Player's FT%");
System.out.println("E.Player's REB");
System.out.println("F.Player's AST");
System.out.println("G.Player's PTS");

if(player.equalsIgnoreCase("A"))
{
    for (int int x=1;x<s.length;++x)
    {
        for (int y=1;y<s.length-1;++y)
                if (s[y][0].compareTo(s[y+1][0])>0)
                {
                    String temp[]=s[y];
                    s[y]= s[y+1];
                    s[y+1]=temp;
                }
            }
        }
    }
}

For example, if user presses a, I want it to sort by player's name only and also I get an error for stats[j][i]=temp[i]; saying that java.lang.ArrayIndexOutOfBoundsException: 6. I don't know why I keep on getting this error?

Below is my data text file, players' names are in column 0, Lou, Kyle, Patrick and more... Because if I just get this I can sort the other ones easily?

Lou Williams,41.1,36.3,87.6,60,53,508
Kyle Lowry,44.9,35.1,81.3,160,260,702
Patrick Patterson,48.8,46.0,75.0,177,61,286
Terrence Ross,42.9,38.7,87.9,119,30,411
Jonas Valanciunas,54.2,0.0,79.2,283,16,414
Amir Johnson,57.8,38.9,60.0,171,47,295
James Johnson,58.3,19.4,65.7,129,46,247
Demar Derozan,39.4,25.0,82.3,67,42,310
Landry Fields,52.9,50.0,83.3,22,13,42
Greivis Vasquez,38.7,31.7,71.1,81,119,321

Upvotes: 2

Views: 652

Answers (3)

Dima
Dima

Reputation: 40500

"i get an error for stats[j][i]=temp[i]; saying that java.lang.ArrayIndexOutOfBoundsException: 6. Idk why i keep on getting this error?"

Because your stats is allocated as String[6][10], so the first index can be in range from 0 to 5, but your j is coming from for (int j=0; j<10; j++), so, when it becomes 6, you get the exception.

Now, if you only want to sort by the first column, you don't really need any splitting etc. Just read stings into a list, and sort.

List<String> strings = new ArrayList<>();
String s = null;
while((s = br.readLine()) != null) strings.add(s);
Collections.sort(strings);

Done.

Upvotes: 2

Niels Billen
Niels Billen

Reputation: 2199

Another answer, but now with the additional constraint that no additional classes should be created (as requested by @Let'sgothebeastboy)

File file = new File("file.txt");
FileReader reader = new FileReader(file);
BufferedReader r = new BufferedReader(reader);

String[][] stats = new String[10][];
String line;
int index = 0;
while ((line = r.readLine()) != null && index < 10)
stats[index++] = line.split(",");

// sort based on the first column
for (int i = 0; i < stats.length; ++i)
    for (int j = i + 1; j < stats.length; ++j)
        if (stats[i][0].compareTo(stats[j][0]) > 0) {
            // swap
            String[] temp = stats[i];
            stats[i] = stats[j];
            stats[j] = temp;
        }
for(int i=0;i<stats.length;++i){
    for(int j=0;j<stats[i].length;++j)
        System.out.print(stats[i][j] + " ");
    System.out.println();
}

Upvotes: 0

Niels Billen
Niels Billen

Reputation: 2199

Lets solve it in an OO way. I created a Statistic class which stores the statistics of one player as an array of Strings and a static method which returns a Comparator to sort Statistic objects along a certain column:

private class Statistic {
    private String[] string;

    public Statistic(String... strings) {
        this.string = strings;
    }

    public String get(int index) {
        return string[index];
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        for (String string : this.string)
            builder.append(string).append(" ");
        return builder.toString();
    }
}

public static Comparator<Statistic> getComparator(final int column) {
    return new Comparator<Statistic>() {
        @Override
        public int compare(Statistic o1, Statistic o2) {
            return o1.get(column).compareTo(o2.get(column));
        }
    };
}

Test code:

File file = new File("file.txt");
FileReader reader = new FileReader(file);
BufferedReader r = new BufferedReader(reader);

List<Statistic> statistics = new ArrayList<Statistic>();
String line;
while ((line = r.readLine()) != null) {
    statistics.add(new Statistic(line.split(",")));
}

int column = 0;
Collections.sort(statistics, getComparator(column));
for (Statistic statistic : statistics)
    System.out.println(statistic);

Output:

Amir Johnson 57.8 38.9 60.0 171 47 295 
Demar Derozan 39.4 25.0 82.3 67 42 310 
Greivis Vasquez 38.7 31.7 71.1 81 119 321 
James Johnson 58.3 19.4 65.7 129 46 247 
Jonas Valanciunas 54.2 0.0 79.2 283 16 414 
Kyle Lowry 44.9 35.1 81.3 160 260 702 
Landry Fields 52.9 50.0 83.3 22 13 42 
Lou Williams 41.1 36.3 87.6 60 53 508 
Patrick Patterson 48.8 46.0 75.0 177 61 286 
Terrence Ross 42.9 38.7 87.9 119 30 411 

Upvotes: 1

Related Questions