-3

Possible Duplicate:
How to sort an arraylist of objects by a property?
What is the sorting algorithm for Java

I have a class in java that keeps record of diffSeconds.

class fileObj
    {
        public String fileName; 
        public Date modificationTime; 
        public long diffSeconds;     

        fileObj()
        {
            modificationTime = null; 
        }
    }

I have a forloop that makes multiple instances of the fileObj class and adds them to a list.

List<fileObj> fileView = new ArrayList<fileObj>();
for(int j=0; j<10; j++)
       {
         fileView.add(new fileObj());
       }

Assuming that each class has a diffSeconds values assigned to them, how can i order the list depending on the value (largest first)

Community
  • 1
  • 1
user648244
  • 1,240
  • 7
  • 26
  • 41
  • @Mike L.: please read the homework tag wiki. – Mat Sep 22 '12 at 10:19
  • possible duplicate of [How to sort an arraylist of objects by a property?](http://stackoverflow.com/questions/2535124/how-to-sort-an-arraylist-of-objects-by-a-property) and [How to sort an array of objects in Java](http://stackoverflow.com/questions/3077746/how-to-sort-an-array-of-objectspoints-in-java?rq=1) – DNA Sep 22 '12 at 19:39

3 Answers3

2

You can use Collections.sort(fileView); and implement Comparable interface and write implemented method compareTo(fileObj arg0) which decide for sorting your list.

For implementation,

class fileObj implements Comparable<fileObj>

@Override
    public int compareTo(fileObj arg0) {

        if(this.diffSeconds > arg0.diffSeconds )
        return 0;
        else return 1;
    }

Reference:

Detail Coding.

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


public class SortExample {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        List<fileObj> fileView = new ArrayList<fileObj>();
        for(int j=0; j<10; j++)
               {
                 fileView.add(new fileObj(j));
               }
        Collections.sort(fileView);

        for(fileObj obj: fileView){
            System.out.println("File " + obj.getDiffSeconds());
        }


    }

}
class fileObj implements Comparable<fileObj>
{
    public String fileName; 
    public Date modificationTime; 
    public long diffSeconds;     

    fileObj()
    {
        modificationTime = null; 
    }

    fileObj(long diffSeconds)
    {
        modificationTime = null;
        this.diffSeconds = diffSeconds;
    }

    @Override
    public int compareTo(fileObj arg0) {

        if(this.diffSeconds > arg0.diffSeconds )
        return 0;
        else return 1;
    }

    public long getDiffSeconds() {
        return diffSeconds;
    }

    public void setDiffSeconds(long diffSeconds) {
        this.diffSeconds = diffSeconds;
    }
}
swemon
  • 5,840
  • 4
  • 32
  • 54
0

use comparator or comparable interfaces.

PermGenError
  • 45,977
  • 8
  • 87
  • 106
0

Look at the link below :-

http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html

Search for the sort method which takes objects as parameter and implement comparable interface in your class.

dareurdream
  • 241
  • 4
  • 13