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 178601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:15:35+00:00 2026-05-11T14:15:35+00:00

Update : In fact, the only acceptable solution for this problem would be sorting

  • 0

Update: In fact, the only acceptable solution for this problem would be sorting the array ascending and then reversing it.

Let S be the following sequence of events:

Event | Time   A   | 0:00   B   | 0:01   C   | 0:01   D   | 0:02 

I have a simple Comparator to sort S, which sorts the elements according to the time value.

public int compare(Event e1, Event e2) {      // Reverse sorting.      // _sortOrder is set outside this method.     if(SORT_DESCENDING.equals(_sortOrder))         return e2.getTime() - e1.getTime(); /* time is long */      return e1.getTime() - e2.getTime(); } 

The problem is: when the sort order is ascending, S is sorted correctly: A, B, C, D.

But when I use reverse sorting, S becomes D, B, C, A:

Event | Time   D   | 0:02   B   | 0:01 /* B and C should be reversed */   C   | 0:01   A   | 0:00 

This happens because the default sorting algorithm keeps the original order for elements with the same time value.

So, how do I sort it reverse without keeping the original order?

Note: I know I can sort S ascending and further simply revert it, but unfortunately this is not an option in my case.

  • 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. 2026-05-11T14:15:35+00:00Added an answer on May 11, 2026 at 2:15 pm

    The sorting algorithm is correct: 0.01 is 0.01. Unless there’s something you’re not telling us. If however you want the exact reverse order of an ascending sort then sort them in ascending order and use Collections.reverse(). By this I mean:

    List<SomeObject> list = ...; Collections.sort(list, myComparator); Collections.reverse(list); 

    which will give you exactly what you want.

    But if reversing isn’t an option you only have two options left:

    1. Make it so no two elements can be ‘equal’. Either include another field or add a synthetic one (such as the current index or the primary key if it is a number). This will give you a reproducible, consistent and mirrored order; or
    2. Implement your own sorting algorithm. This is not recommended as it’s simply too error prone.

    Now before you say you can’t reverse (why?), let me ask you how you’re sorting? if you’re using Collections.sort() consider the source code (Java 6u10):

    public static <T extends Comparable<? super T>> void sort(List<T> list) { Object[] a = list.toArray(); Arrays.sort(a); ListIterator<T> i = list.listIterator(); for (int j=0; j<a.length; j++) {     i.next();     i.set((T)a[j]); } } 

    So it copies the collection into an array, sorts that array and then uses that to reorder the collection.

    Are you sure you can’t afford a reversal?

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

Sidebar

Ask A Question

Stats

  • Questions 102k
  • Answers 102k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Extracting the game type from the host's game name will… May 11, 2026 at 8:15 pm
  • Editorial Team
    Editorial Team added an answer You can set the background color for a static control… May 11, 2026 at 8:15 pm
  • Editorial Team
    Editorial Team added an answer Make sure to reference System.Linq and include it (using System.Linq)… May 11, 2026 at 8:15 pm

Related Questions

I have been getting more and more sick of all the quirks about control
At the moment, our application uses the Trident Win32 component, but we want to
Background I work for a large organization which has thousands of MS Access applications
I have been looking at the examples for updating models in the MVC architecture.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.