Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8157577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:19:56+00:00 2026-06-06T17:19:56+00:00

I am trying to sort a Vector list using the Java Collections frameworks. This

  • 0

I am trying to sort a Vector list using the Java Collections frameworks.

This is my code…

public class RiverList {

    public static void main(String[] args) {
        Vector<River> rivers = new Vector<River>();
        //name length and discharge

        River river3 = new River("Nil", 6852, 2660);
        River river5 = new River("Amazonas", 6448, 209000);
        River river1 = new River("Rhein", 1233, 2330);
        River river4 = new River("Donau", 2857, 6700);
        River river2 = new River("Oker", 128, 12);

        rivers.add(river1);
        rivers.add(river2);
        rivers.add(river3);
        rivers.add(river4);
        rivers.add(river5);

        System.out.println(rivers);

        // sort the river after length with help class collection

        System.out.println(rivers);

        // sort the river after discharge with help class collection

        System.out.println(rivers);
    }
}

public class River {
    private int length;
    private int discharge;
    private String name; 

    public River(String name, int length, int discharge) {
        this.name = name;
        this.length = length;
        this.discharge = discharge;
    }

    // getters and setters
    ...

    public String toString() {
        return name + ", Length = " + length + ", discharge = " + discharge + "\n";
    }
}

Here is what I have done, but I am stuck on how to proceed further…

public class RiverLengthComparator implements Comparator {
    public int compare(Object arg0, Object arg1) {
        return 0;
    }
}

public class RiverDischargeComparator implements Comparator {
    public int compare(Object o1, Object o2) {
        return 0;
    }
}

The output should be like this (for river length)…

("Nil", 6852, 2660);
("Amazonas", 6448, 209000);
("Donau", 2857, 6700);
("Rhein", 1233, 2330);
("Oker", 128, 12);

And for river discharge…

("Amazonas", 6448, 209000);
("Donau", 2857, 6700);
("Nil", 6852, 2660);
("Rhein", 1233, 2330);
("Oker", 128, 12);

Could someone please help me with this problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:19:57+00:00Added an answer on June 6, 2026 at 5:19 pm

    maybe your compare method should return something else than 0 😉
    Think about what you want to achieve and read the documentation of the Comparator interface.

    maybe using Comparator<River> instead of just Comparator would be a first step.

    In the end you might want to use Collections.sort(rivers, new RiverLengthComparator()); with the comparator you want to use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to sort a vector below using boost::phoenix library. The class Foo
I'm trying to sort a Vector in java but my Vector is not a
Within a class, I am trying to sort a vector, by passing a method
I'm trying to compile this code with GCC 4.5.0: #include <algorithm> #include <vector> template
I'm trying to sort a vector of objects using a predicate function and I'm
I was trying to sort a vector of struct.The following is my code.I am
I am trying to create a custom sort for a vector of class pointers
Trying to sort this array of objects according to (1) depth and (2) weight,
Been trying to sort this one out for a while. I'd really appreciate any
I am trying to sort a list of names followed by another string such

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.