954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help sorting object vector

Hi All,

I could really use some help in sorting vectors, i know the collections.sort(x) method will sort a vector of numbers etc however i have a vector full of objects and i need to sort them; the object contains details of a website please find the fields below;

private String shortName;
    private String contact;
    private String URL;
    private String country;
    private String dateAccessed;
    private String image;
    private double rank;
    private int popularity;
    private Vector<String> keywords;

I need to sort the vector holding (multiple of these objects) by the following criteriaDisplay results sorted by rank (1 being the highest rank)
Display results sorted by popularity (largest to smallest)
Display results sorted by date accessed (most recent 1st)
Could somebody please let me know the most efficient way of doing this with some examples thanks

Regards,
Sinister747

sinister747
Newbie Poster
14 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 
BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

If you want to sort the vector according to your own ordering This ordering is done by defining a class (for each criteria for exemple) that implements the java.util.Comparator interface, returning < 0, 0, or > 0 according to whether the first element is less than, equal to, or greater than the second.
And the call the sort method of the sort’s collections function and pass to it you class comparer:

I made for you an example that I will send in separates posts; this is not a unique or best solution:

(continued....)

moutanna
Posting Whiz
387 posts since Oct 2009
Reputation Points: 16
Solved Threads: 58
 

Website Classe:

import java.util.Vector;

/**
 *
 * @author Aziz
 */
public class Website {

    private String shortName;
    private String contact;
    private String URL;
    private String country;
    private String dateAccessed;
    private String image;
    private double rank;
    private int popularity;
    private Vector<String> keywords;

    public Website(String shortName,  String dateAccessed, double rank, int popularity) {
        this.shortName = shortName;
        this.dateAccessed = dateAccessed;
        this.rank = rank;
        this.popularity = popularity;
    }

    public Website() {

    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    public double getRank() {
        return rank;
    }

    public void setRank(double rank) {
        this.rank = rank;
    }

    public int getPopularity() {
        return popularity;
    }

    public void setPopularity(int popularity) {
        this.popularity = popularity;
    }

    public String getDateAccessed() {
        return dateAccessed;
    }

    public void setDateAccessed(String dateAccessed) {
        this.dateAccessed = dateAccessed;
    }
    
}

(continued..)
moutanna
Posting Whiz
387 posts since Oct 2009
Reputation Points: 16
Solved Threads: 58
 

Website Classe:

import java.util.Vector;

/**
 *
 * @author Aziz
 */
public class Website {

    private String shortName;
    private String contact;
    private String URL;
    private String country;
    private String dateAccessed;
    private String image;
    private double rank;
    private int popularity;
    private Vector<String> keywords;

    public Website(String shortName,  String dateAccessed, double rank, int popularity) {
        this.shortName = shortName;
        this.dateAccessed = dateAccessed;
        this.rank = rank;
        this.popularity = popularity;
    }

    public Website() {

    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    public double getRank() {
        return rank;
    }

    public void setRank(double rank) {
        this.rank = rank;
    }

    public int getPopularity() {
        return popularity;
    }

    public void setPopularity(int popularity) {
        this.popularity = popularity;
    }

    public String getDateAccessed() {
        return dateAccessed;
    }

    public void setDateAccessed(String dateAccessed) {
        this.dateAccessed = dateAccessed;
    }
    
}

(continued..)
moutanna
Posting Whiz
387 posts since Oct 2009
Reputation Points: 16
Solved Threads: 58
 

Websites class:

import java.util.Vector;

/**
 *
 * @author Aziz
 */

public class Websites {

    private Vector<Website> sites=new Vector<Website>();

    public Vector<Website> getSites() {
        return sites;
    }
    public void addWebsite(Website website){
        this.sites.add(website);
    }
}

(continued...)
moutanna
Posting Whiz
387 posts since Oct 2009
Reputation Points: 16
Solved Threads: 58
 

Comparators class:

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

/**
 *
 * @author Aziz
 */
public class Comparers /*implements Comparator*/ {

    /**
     * ComparerByrange compare class by there range in th natural way
     */
    public class ComparerByrange implements Comparator {

        public int compare(Object o1, Object o2) {
            Website wbs1 = (Website) o1;
            Website wbs2 = (Website) o2;
            /**
             * sorts the vector in a natural way, that is, from lowest to highest according to the values contained in the wrappers.highest
             */
            int i = 0;
            if (wbs1.getRank() < wbs2.getRank()) {
                i = -1;
            }
            if (wbs1.getRank() == wbs2.getRank()) {
                i = 0;
            }
            if (wbs1.getRank() > wbs2.getRank()) {
                i = 1;
            }
            return i;
        }
    }
    /**
     * declore more class that support more creteries;
     */

     public Comparator getComparerByrange(){
        return new ComparerByrange();
    }


    public class ComparerByPopularity implements Comparator {

        public int compare(Object o1, Object o2) {
            //put your the code here.
            throw new UnsupportedOperationException("Not supported yet.");
        }

    }

    /**
     * declare the setter tho ge comparer
     */
    public Comparator getComparerByPopularity(){
        return new ComparerByPopularity();
    }

    /**
     * Another comparer
     */
    public class ComparerByDateAccessed implements Comparator {

        public int compare(Object o1, Object o2) {
            //put your the code here.
            throw new UnsupportedOperationException("Not supported yet.");
        }

    }
     /**
     * declare the setter tho ge comparer
     */
    public Comparator getComparerByDateAccessed(){
        return new ComparerByDateAccessed();
    }
}


(continued...)

moutanna
Posting Whiz
387 posts since Oct 2009
Reputation Points: 16
Solved Threads: 58
 

client test class:

import java.util.Collections;

/**
 *
 * @author Aziz
 */
public class Main {

    public Main() {
        Comparers comparers = new Comparers();
        Websites websites = new Websites();
        Website website = new Website();
        //First website
        website.setShortName("Site 1");
        website.setRank(12);
        websites.addWebsite(website);
        //set moer properties here


        //Second website
        website = new Website();
        website.setShortName("Site 1");
        website.setRank(10);
        websites.addWebsite(website);
        Collections.sort(websites.getSites(), comparers.getComparerByrange());
        //testing the result
        for (int i = 0; i < websites.getSites().size(); ++i) {
            System.out.println(websites.getSites().get(i).getRank());
        }
    }

    public static void main(String[] args) {
        new Main();
    }
}


(end of class);
Just copy past the calss above in the same package and run the application.

Hope it helps.

moutanna
Posting Whiz
387 posts since Oct 2009
Reputation Points: 16
Solved Threads: 58
 

Follow Moutanna's suggestion to use Comparators. I misread your post the first time around; I thought you wanted to sort based on "1" then sort based on "2" if there was a tie, etc. But what you actually want to do is sort three different ways, so follow Moutanna's suggestion and use three different Comparators.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

Great thanks for the help guys i will give it a try, and wow thats alot of code you wrote thanks lol

sinister747
Newbie Poster
14 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

Hapy to help!

moutanna
Posting Whiz
387 posts since Oct 2009
Reputation Points: 16
Solved Threads: 58
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You