Sal
Sal

Reputation: 109

Sorting objects in alphabetical order

For my assignment, I have to create both a method to sort integers and Strings stored in an object class. Keep in mind, I HAD TO USE CASTS. I wanted to use generics, but my teacher INSISTS on me using 1.4.2 (which don't have generics). I can sort time, and for the alphabetical sort, I used my method to sort time and added a compareTo. I played with it a bit, but when I output it, it gives me everything I inputted in the order I inputted it. Not in alphabetical.

Here's the class I created to store input:

public class showInfo 
{
    String name;
    String day; 
    int time;    
}

The following is the method to sort by name!

//method to sort and display info
public static void sortName(){          
    for(int i = 0; i < show.size() - 1; i++) {
        for(int j = 0; j < show.size() - 1; j++){
            if(((showInfo)show.get(i)).name.compareTo(((showInfo)show.get(i+1)).name) > 0){
                showInfo temp = new showInfo();
                temp.name = ((showInfo)show.get(j)).name;
                temp.day = ((showInfo)show.get(j)).day;
                temp.time = ((showInfo)show.get(j)).time;

                ((showInfo)show.get(j)).time = ((showInfo)show.get(i)).time;
                ((showInfo)show.get(j)).day = ((showInfo)show.get(i)).day;
                ((showInfo)show.get(j)).name = ((showInfo)show.get(i)).name;

                ((showInfo)show.get(i)).time = temp.time;
                ((showInfo)show.get(i)).day = temp.day;
                ((showInfo)show.get(i)).name = temp.name;
            }
        } 
    } 

Any help would be great! Thanks in advance. :)

(PS. I'm aware I need to change "showInfo" to "ShowInfo", but I'll do it when I'm finished.)

Upvotes: 0

Views: 778

Answers (5)

kajacx
kajacx

Reputation: 12939

Not sure if this is what you look for, it uses casting insteads of generics, anyway, i hope this will help

pastebin

Upvotes: 0

Abed Yaseen
Abed Yaseen

Reputation: 522

You are incorrectly using i here :

if(((showInfo)show.get(i)).name.compareTo(((showInfo)show.get(i+1)).name) > 0){

I believe the second i should be j to achieve bubble sort

if(((showInfo)show.get(i)).name.compareTo(((showInfo)show.get(j+1)).name) > 0){ 

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

You can do something like this....

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class myComparator implements Comparator {

    public int compare(Object o1, Object o2) {
        return ((o1.toString().charAt(0) > o2.toString().charAt(0)) ? 1 : (o1
                .toString().charAt(0) == o2.toString().charAt(0)) ? 0 : -1);
    }
}

public class Sample {

    /**
     * @param args
     */
    public static void main(String[] args) {
        List l = new ArrayList();
        l.add("hello");
        l.add("abc");
        l.add("World");
        l.add("hi");

        System.out.println("Before sorting");
        for (Object i : l) {
            System.out.println(i.toString());
        }

        Collections.sort(l, new myComparator());

        System.out.println("After sorting");
        for (Object i : l) {
            System.out.println(i.toString());
        }

    }

}

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234807

One problem with your code is that you are comparing show.get(i) with show.get(i+1) but then swapping show.get(i) with show.get(j). You should be comparing to show.get(j). Also, the inner loop should go to j < show.size() rather than show.size() - 1. Finally, you can start the inner loop at i + 1 instead of at 0.

Once you determine that you need to swap, you can do much better by simply swapping references in the list, rather than swapping each field:

showInfo tmp = (showInfo)show.get(i);
show.set(i, show.get(j));
show.set(j, tmp);

Upvotes: 3

Gabriel Negut
Gabriel Negut

Reputation: 13960

I assume show is a List and you have to sort by name.

First, make showInfo implement Comparable:

public class showInfo implements Comparable
{
    String name;
    String day; 
    int time;

    public int compareTo(Object o)
    {
        showInfo other = (showInfo) o;
        return name.compareTo(other.name);
    }  
}

Then, use `Collections.sort()' on the list:

Collections.sort(show);

Upvotes: 1

Related Questions