1

Im trying to implement a sort method into my program following the advice from Sort an arraylist of objects in java but could not get this working. I get the error "Cannot find symbol - method sort()"

Appologies im new to java..

Entry Class:

/**
 * The Entry class represents a persons address entry into the system.
 * It holds the persons name and address information
 * 
 */
public class Entry
{
// the person's full name
private String name;
// the steet namd or number
private String street;
// the town
private String town;
// postcode
private String postcode;

/**
 * Create a new entry with a given name and address details.
 */
public Entry(String strName, String strStreet, String strTown, String strPostcode)
{
    name = strName;
    street = strStreet;
    town = strTown;
    postcode = strPostcode;
}

/**
 * Return the address for this person. The address shows the
 * street, town and postcode for the person
 * @return address
 */
public String getAddress()
{
    return name + ", " + street + ", " + town + ", " + postcode;
}

}

AddressBook class:

import java.util.*;

/**
 * The AddressBook class represents an address book holding multiple persons details. It stores
 * the name, street, town, postcode and number of entries.
 */
public class AddressBook
{
private ArrayList < Entry > entries;

/**
 * Create a an AddressBook with no limit on the number of entries
 */
public AddressBook()
{
    entries = new ArrayList < Entry >();
}

 /**
 * Return the number of address book entries
 * @return the entry amount
 */
 public String getAddressBook()
{
    String listEntries = "Address Book\n";

    listEntries = listEntries + "\nNumber of entries: " + numberOfEntries() +"\n";

    entries.sort();

    for (int i = 0; i < entries.size(); i++) {
           System.out.println(entries.get(i));
          }
    return listEntries;
}

AddressBookTextUI Class:

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
* Provides a text based user interface for the AddressBook project.
* 
*/
public class AddressBookTextUI {
private AddressBook addressBook;
Scanner myScanner;

/**
 * Constructor for objects of class TextUI
 */
public AddressBookTextUI()
{
    addressBook = new AddressBook();
    myScanner = new Scanner(System.in);

}



private void fullCommand()
{
    System.out.println(addressBook.getAddressBook());
    clearScreen();
}

}
1
  • Next time, post the relevant part of the code, instead of posting everything you have coded. Commented Mar 1, 2013 at 8:14

3 Answers 3

4

Assuming you are referring to this: entries.sort();, you would need to call Collections.sort(List<T> list) or Collection.sort(List<T> list, Comparator<T> comparator).

The difference between the two is that the first one uses a default behaviour, which can be specified in the object you will be sorting by implementing the compareTo method specified by the Comparable interface while the second allows you to pass a comparator object. This would allow you to sort the same list differently.

0
3

You need to use Collections.sort(List<T> list).

List is an interface. ArrayList indirect implements List interface.

4
  • so instead of entries.sort I would use Collections.sort(List<entries> list) ? Commented Mar 1, 2013 at 8:21
  • @ToniHopkins: Yes. The method modifies the same collection so it doesn't return anything. Once you call that method your collection will be sorted. Commented Mar 1, 2013 at 8:27
  • @npinti I just get an error saying cannot find symbol - variable List Commented Mar 1, 2013 at 8:31
  • @ToniHopkins: You do not need to pass the List<entries> segment, that is just part of the method's signature. Try calling it like so: Collections.sort(entries), where entries is the collections you would like to sort. Commented Mar 1, 2013 at 8:42
1
  1. Make Entry implements Comparable:

    public class Entry implements Comparable

  2. Add int compareTo(Entry otherEntry) method (simplified pseudocode):

    if this > otherEntry return 1
    else if this < otherEntry return -1
    else return 0
    
  3. Call Collections.sort(your array of Entries)

3
  • I followed youradvice and created the compareTo method however I get the following error: Bad operand types for binary operator '>' first type: Entry; second type: Entry Commented Mar 1, 2013 at 9:02
  • Cause it's not valid java code, it's pseudocode. You have to write your own content which compares two Entries. Commented Mar 1, 2013 at 9:16
  • Sorry I should of said I used the name to try and compare and got this error. I have found another post that uses names to create a compareTo method under stackoverflow.com/questions/10017381/compareto-method-java so I will try using examples from here. Thank you Commented Mar 1, 2013 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.