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

  • SEARCH
  • Home
  • 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 8673267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:24:54+00:00 2026-06-12T19:24:54+00:00

Okay suppose I have an array of objects that look like this: obj(from, to)

  • 0

Okay suppose I have an array of objects that look like this:
obj(from, to)
I want to to sort that array by comparing from and to. An example of what I want done:
Suppose I have objects with these parameters
(0,2) (2,4) (0,3) (4,5) (2,3)
I want the objects to be sorted in this order:
(0,2) (0,3) (2,3) (2,4) (4,5)

I want the first two “from” variables compared and the lower one placed in front. If they are equal then I want the second pair of number compared. To do this I created a compare method

public int compare (EdgeI e1, EdgeI e2) {
  if(e1.from < e2.from) { return -1; }
  else if(e1.from == e2.from) {
    if(e1.to < e2.to) { return -1; }
    else if(e1.to == e2.to) { return 0; }
    else if(e1.to > e2.to) { return 1; }
  }
  return 1;
}

Would this work? And if so, how would I run this sort through the array?
Thanks for any help.

EDIT

    public class mySorter implements Comparator <EdgeI> {

  public int compare(EdgeI e1, EdgeI e2) {
    if(e1.from < e2.from) { return -1; }
    else if(e1.from == e2.from) {
      if(e1.to < e2.to) { return -1; }
      else if(e1.to == e2.to) { return 0; }
      else if(e1.to > e2.to) { return 1; }
    }
    return 1;
  }

  public void sorterM () {
    Collections.sort(tet2, new mySorter());
  }

}

I get the error Collections cannot be resolved, and tet2 cannot be resolved. Tet2 is a List delcared public in another class.

  • 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-12T19:24:56+00:00Added an answer on June 12, 2026 at 7:24 pm

    What you can do is create a class which implements Comparator<Edge>. You can then use your compare method to implement the method from the interface.

    After you have done this you can use the comparator to sort a list of Edge objects using Collections.sort().

    This would look something like this:

    import java.util.Collections;
    import java.util.List;
    import java.util.Comparator;
    
    public class EdgeComparator implements Comparator<Edge> {
        public int compare(Edge l, Edge r) { ... }
    }
    
    void yourCode() {
        List<Edge> edges = ...;
        Collections.sort(edges, new EdgeComparator());
        //edges now contains the sorted edges
    }
    

    Here is the javadoc on Comparator and Collections.sort.

    If you have an array instead of a list you can instead use Array.sort in the same way as Collections.sort.

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

Sidebar

Related Questions

Okay, so, I'm doing a simple lookup. I have an array of NSString objects
I have a array which contains values like this See below for script sample
Suppose that I have a date Jan 31 2011 and I want to add
suppose we have strings like adjkadahsdjashd##kjhkhkjhkjhs . and I want to cut them to
Suppose I have a query like this: SELECT s.staffID as staffID, CONCAT_WS(, , lname,
Okay, next PHPExcel question. I have an HTML form that users fill out and
Okay this question is very simple: I have a facebook page, and a website.
okay, i'm setting up a multi-user chat system. i have a messages table, that
Okay, suppose I have a bunch of variables, one of them declared volatile: int
Suppose I have some code, struct NodeVector { vector<bool_node*> *vec; }; I want to

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.