-3

public class Lot implements Listable {

int EmpID;
String Ename;
double Sal;

public Lot(int id,String ename, double sal) {
   this.EmpID = id;
   this.Ename = ename;
   this.Sal = sal;
}

public int getEmpID() {
    return EmpID;
}

public String getEname() {
    return Ename;
}

public double getSal() {
    return Sal;
}

public String toString() {
    return "ID - " + EmpID + "\n" + "Name - " + Ename + "\n" + "Salary - " + Sal;
}

@Override
public int compareTo(Listable otherList) {
    Lot other = (Lot)otherList;
    return (this.EmpID - other.EmpID);
}

}

Main Class :

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

public class SortTest {

public static void main(String[] args) {

    List list = new ArrayList();

    list.add(new Lot(4, "aaa", 12000));
    list.add(new Lot(3, "bbb", 1000));
    list.add(new Lot(1, "ccc", 8000));
    list.add(new Lot(2, "ddd", 2500));

    Collections.sort(list); //ERROR 
}

}
This gives an Error 'Lot cannot be cast to java.lang.Comparable'
What is the mistake...

  • 1
    Create a class that can house the data, create an instance of the class for each entry, add them to a list, post the code, and then we can talk sorting. – Ali Cheaito Mar 11 '15 at 13:42
  • 1
    Create a class having mentioned attributes, implement Comparable interface, store objects in a TreeSet – Kartic Mar 11 '15 at 13:42
  • 1
    You're getting downvoted because all you've done is dump some data on here and ask how to sort it without showing any effort or coming up with ideas of your own. [What Have You Tried](http://whathaveyoutried.com) so far? – David Mar 11 '15 at 13:44
  • ok I'll do it & thnkz for the advices – user3904560 Mar 11 '15 at 15:33
  • Did you try this? http://xkcd.com/1185/ – James Kingsbery Mar 11 '15 at 21:08

4 Answers4

1

I just add the instruction not the code implementation -

  1. Create a class Lot with lotNum and other property. Implement the interface comparable interface for this class.
  2. Add all these lot to collection like ArrayList - lotList
  3. Now call the Collections.sort()

Hope it will help.
Thanks a lot.

Community
  • 1
  • 1
Razib
  • 10,965
  • 11
  • 53
  • 80
1

Create a Class which implements Comparable interface like below :

public class Lot implements Comparable<Lot> {

private int lotNumber;
private String firstName;
private String lastName;
private BigDecimal price; 
private Long squareFeet;
private int numberofBedRooms;


public int getLotNumber() {
    return lotNumber;
}


public void setLotNumber(int lotNumber) {
    this.lotNumber = lotNumber;
}


public String getFirstName() {
    return firstName;
}


public void setFirstName(String firstName) {
    this.firstName = firstName;
}


public String getLastName() {
    return lastName;
}


public void setLastName(String lastName) {
    this.lastName = lastName;
}


public BigDecimal getPrice() {
    return price;
}


public void setPrice(BigDecimal price) {
    this.price = price;
}


public Long getSquareFeet() {
    return squareFeet;
}


public void setSquareFeet(Long squareFeet) {
    this.squareFeet = squareFeet;
}


public int getNumberofBedRooms() {
    return numberofBedRooms;
}


public void setNumberofBedRooms(int numberofBedRooms) {
    this.numberofBedRooms = numberofBedRooms;
}


@Override
public int compareTo(Lot lot) {
    return this.lotNumber - lot.lotNumber;
    }   
}

After this create a List of this object with all the values populated and use Collections.sort(---).

Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
Aalekh
  • 400
  • 2
  • 7
  • 12
  • Btw, it is also possible to pass a custom comparator to `Collections.sort()` or even use method reference to `List.sort()` if you use Java 8. – Alex Salauyou Mar 11 '15 at 14:03
  • @Shasha Salauyou --- could you please give some reference. – Aalekh Mar 11 '15 at 14:05
  • http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-java.util.Comparator- and http://docs.oracle.com/javase/8/docs/api/java/util/List.html#sort-java.util.Comparator- – Alex Salauyou Mar 11 '15 at 14:07
  • In Java 8, just use `yourList.sort(comparing(Lot::getLotNumber))` – Alex Salauyou Mar 11 '15 at 14:10
  • yourList.sort(comparing(Lot::getLotNumber)) --where is this format of code mentioned in the above two links? – Aalekh Mar 11 '15 at 14:14
  • In second one, but not directly. In Java 8, some convenience methods were added to interfaces. In this case, `List#sort()` method accepts `Comparator`, which is functional interface and thus can be replaced by method reference. – Alex Salauyou Mar 11 '15 at 14:18
  • http://www.baeldung.com/java-8-sort-lambda #6 – Alex Salauyou Mar 11 '15 at 14:22
0

An example of how you can do :

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

public class TestJava { 
    public static void main(String[] args) throws java.text.ParseException {

        List<Lot> lots = new ArrayList<Lot>();

        lots.add(new Lot(2, "bbb", "ccc", "bbb", "sss", "ccc"));
        lots.add(new Lot(4, "bbb", "ccc", "bbb", "sss", "ccc"));
        lots.add(new Lot(3, "bbb", "ccc", "bbb", "sss", "ccc"));
        lots.add(new Lot(1, "bbb", "ccc", "bbb", "sss", "ccc"));

        Collections.sort(lots);

     }
}

Create Lot class :

class Lot implements Comparable<Lot> {

    public int lotNumber;
    public String firstName;
    public String lastName;
    public String price;
    public String squareFeet;
    public String noOfBedRooms;

    public Lot(int lotNumber, String firstName, String lastName, String price, String squareFeet, String noOfBedRooms) {
        this.lotNumber = lotNumber;
        this.firstName = firstName;
        this.lastName = lastName;
        this.price = price;
        this.squareFeet = squareFeet;
        this.noOfBedRooms = noOfBedRooms;
    }

    public int getLotNumber() {
        return lotNumber;
    }
    public void setLotNumber(int lotNumber) {
        this.lotNumber = lotNumber;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public String getSquareFeet() {
        return squareFeet;
    }
    public void setSquareFeet(String squareFeet) {
        this.squareFeet = squareFeet;
    }
    public String getNoOfBedRooms() {
        return noOfBedRooms;
    }
    public void setNoOfBedRooms(String noOfBedRooms) {
        this.noOfBedRooms = noOfBedRooms;
    }

    @Override
    public int compareTo(Lot lot) {
        return this.getLotNumber() - lot.getLotNumber();
    }
}

Change type of the attributes as you wish.

After the post I realized this is same answer as @Aalekh :)

Kartic
  • 2,935
  • 5
  • 22
  • 43
0

there is a lot of sorting method that you can implement such as heap sort and bubble sort then you can implement substring in order to reorganize your data :d

for example you can user heap sort , `public static void BubbleSort( int [ ] num )

{
     int j;
     boolean flag = true;   // set flag to true to begin first pass
     int temp;   //holding variable

     while ( flag )
     {
            flag= false;    //set flag to false awaiting a possible swap
            for( j=0;  j < num.length -1;  j++ )
            {
                   if ( num[ j ] < num[j+1] )   // change to > for ascending sort
                   {
                           temp = num[ j ];                //swap elements
                           num[ j ] = num[ j+1 ];
                           num[ j+1 ] = temp;
                          flag = true;              //shows a swap occurred  
                  } 
            } 
      } 
} 

`

it will take parameters (numbers) sort them and then you use substring ;)

Crystal
  • 13
  • 5