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 criteria

  1. Display results sorted by rank (1 being the highest rank)
  2. Display results sorted by popularity (largest to smallest)
  3. 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

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....)

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..)

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..)

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...)

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...)

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.

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.

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

Hapy to help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.