-4

I have an array of objects which contain a number of fields. I have a class with the constructors, setters and getters. One of the fields is name. I want to sort alphabetically all the names in the array and list them. Can anyone be of help? This is in java.

Thanks

Alex
  • 1
  • 1
  • Maybe. In what language? What kinds of objects are we talking about? – Parthian Shot Oct 11 '16 at 07:03
  • First off: What language are you coding in? Second: Post some code, with your attempt. This way we can help you. [How to ask questions](http://stackoverflow.com/help/how-to-ask) – Roy123 Oct 11 '16 at 07:03
  • 1
    In how many different languages and for how many possible kinds of arrays/objects would you like your answer…? – deceze Oct 11 '16 at 07:05
  • If it's missing a language, somehow it's usually Java… – deceze Oct 11 '16 at 08:23
  • And there are certainly plenty of questions in [tag:java] about sorting arrays which have already been answered. How about http://stackoverflow.com/questions/12449766/java-sorting-sort-an-array-of-objects-by-property-object-not-allowed-to-use-co ? – deceze Oct 11 '16 at 08:26

1 Answers1

0

You can do so with the Comparable interface. Lets assume we have a Class Car, which can be of a certain brand (name in this example)

public class Car implements Comparable<Car> {


    private String name;
    private String color;   

    /**
     * Names
     */
    public enum Name {

        FIAT("fiat"), RENAULT("renault"), HONDA("honda");

        private final String name;      

        private Name(final String name) {
            this.name = name;
        }

        public String toString() {
            return this.name;
        }
    }

    /**
     * Colors
     */
    public enum Colors {

        BLUE("blue"), RED("red"), YELLOW("yellow");

        private final String color;     

        private Colors(final String color) {
            this.color = color;
        }

        public String toString() {
            return this.color;
        }
    }

    /**
     * Construct car with name and color
     * @param name
     * @param color
     */
    public Car(Name name, Colors color) {
        this.name = name.toString();
        this.color = color.toString();
    }

    /**
     * return name
     * @return
     */
    public String getName() {
        return this.name;
    }

    /**
     * compare to other car
     */
    @Override
    public int compareTo(Car car2) {
        return this.name.compareTo(car2.getName());
    }
}

And the main:

import java.util.Arrays;

public class Main {

    //private static List<Car> cars = new ArrayList<Car>();
    private static Car[] cars = new Car[3];

    /**
     * @param args
     */
    public static void main(String[] args) {

        System.out.println("Create cars in array");
        cars[0] = new Car(Car.Name.RENAULT, Car.Colors.YELLOW);
        cars[1] = new Car(Car.Name.HONDA, Car.Colors.BLUE);
        cars[2] = new Car(Car.Name.FIAT, Car.Colors.RED);

//      System.out.println("Create cars in arraylist");     
//      cars.add(new Car(Car.Name.RENAULT, Car.Colors.YELLOW));
//      cars.add(new Car(Car.Name.HONDA, Car.Colors.BLUE));
//      cars.add(new Car(Car.Name.FIAT, Car.Colors.RED));

        System.out.println("Print cars");
        for (Car car : cars) {
            System.out.println("Car name: "+car.getName());
        }

        // sort cars, see Car class
        Arrays.sort(cars);

        System.out.println("Print cars after sort");
        for (Car car : cars) {
            System.out.println("Car name: "+car.getName());
        }
    }
}

See the console for output.

vaultboy
  • 173
  • 1
  • 1
  • 12
  • I don't understand, my objects are stored in array: movies[] movieArr = new movies[0]; – Alex Oct 11 '16 at 10:15