-1

I have an arrayList of type Gift each Present object has fields String name, String type , double Cost

I want to find a way to sort the arrayList using the cost of each present

the language I am using is Java

how do I do this please

Amy
  • 1
  • 2
    possible duplicate of [Sorting an ArrayList of Contacts based on name?](http://stackoverflow.com/questions/1814095/sorting-an-arraylist-of-contacts-based-on-name) – August Jan 01 '15 at 16:36

3 Answers3

1

I want to find a way to sort the arrayList using the cost of each present

You need to either:

  1. Implement Comparable in your custom Present class
  2. Create a custom Comparator to use with the Collection.sort(...) method

Here is my example that demonstrates both approaches:

/*
**  Use the Collections API to sort a List for you.
**
**  When your class has a "natural" sort order you can implement
**  the Comparable interface.
**
**  You can use an alternate sort order when you implement
**  a Comparator for your class.
*/
import java.util.*;

public class Person implements Comparable<Person>
{
    String name;
    int age;

    public Person(String name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public String getName()
    {
        return name;
    }

    public int getAge()
    {
        return age;
    }

    public String toString()
    {
        return name + " : " + age;
    }

    /*
    **  Implement the natural order for this class
    */
    public int compareTo(Person p)
    {
        return getName().compareTo(p.getName());
    }

    static class AgeComparator implements Comparator<Person>
    {
        public int compare(Person p1, Person p2)
        {
            return p1.getAge() - p2.getAge();
        }
    }

    public static void main(String[] args)
    {
        List<Person> people = new ArrayList<Person>();
        people.add( new Person("Homer", 38) );
        people.add( new Person("Marge", 35) );
        people.add( new Person("Bart", 15) );
        people.add( new Person("Lisa", 13) );

        // Sort by natural order

        Collections.sort(people);
        System.out.println("Sort by Natural order");
        System.out.println("\t" + people);

        // Sort by reverse natural order

        Collections.sort(people, Collections.reverseOrder());
        System.out.println("Sort by reverse natural order");
        System.out.println("\t" + people);

        //  Use a Comparator to sort by age

        Collections.sort(people, new Person.AgeComparator());
        System.out.println("Sort using Age Comparator");
        System.out.println("\t" + people);

        //  Use a Comparator to sort by descending age

        Collections.sort(people, Collections.reverseOrder(new Person.AgeComparator()));
        System.out.println("Sort using Reverse Age Comparator");
        System.out.println("\t" + people);
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
0

Have you looked at the comparable interface. http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html?

Maybe have a look at the following question to see how to implement this interface. https://stackoverflow.com/questions/3718383/java-class-implements-comparable

Community
  • 1
  • 1
0

you can use Comparable or Comparator. In your case Comparable is preferable.

gifpif
  • 4,507
  • 4
  • 31
  • 45