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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:17:44+00:00 2026-06-06T02:17:44+00:00

Firstly, this is going to sound like homework, but it ain’t. Just a problem

  • 0

Firstly, this is going to sound like homework, but it ain’t. Just a problem I’m trying to solve at work.

I have a list of objects, the objects have a sequence number which indicates their order in the list in the UI. Example:

public class Task {
  Long id;
  String name;
  Long seq;
}

The table in my UI has “up” and “down” links on each row of the table for moving the tasks up and down in the list.

I’m implementing two methods to handle the reordering. These methods are call by ajax in the web UI.

public void incTaskSeq(List<Task> allTasks, Task taskToMove)

For example; if I have t1.seq=1, t2.seq=2, t3.seq=3, t4.seq=4, t5.seq=5 and I want to increment the place of t3, then t3.seq becomes 4, and t4.seq must become 3.

public void decTaskSeq(List<Task> allTasks, Task taskToMove)

Similarly; if I have t1.seq=1, t2.seq=2, t4.seq=3, t3.seq=4, t5.seq=5 and I want to decrement the place of t4, then t4.seq becomes 2, and t2.seq must become 3. Resulting in:

t1.seq=1, t4.seq=2, t2.seq=3, t3.seq=4, t5.seq=5

I’m a little stuck on the best way to do this.

I was thinking of putting all the tasks in a HashMap and then sort the map by the sequence number. Then locate the taskToMove in the map, change the sequence number, and then change all the affected tasks sequences.

But this approach seems inelegant. Does anyone have any ideas how I should do this?

Thanks, Rob

  • 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-06T02:17:45+00:00Added an answer on June 6, 2026 at 2:17 am

    Use a Comparator, which is the Java interface for sorting non-naturally.

    public TaskSequenceComparator implements Comparator<Task> {
    
      public int compare(Task one, Task two) {
        return one.getSequence() - two.getSequence();
      }
    
    }
    
    ...
    
    List<Task> tasks = ...;
    Collections.sort(tasks, new TaskSquenceComaprator());
    // tasks is now sorted by sequence.
    

    You can create multiple Comparator classes to implement each kind of supported sort. Then you can select from them when you need the list sorted in a particular manner.

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

Sidebar

Related Questions

Firstly: I am totally a newbie for this kind of work. I have a
Firstly, This might seem like a long question. I don't think it is... The
Firstly, I do not have any malicious intent out of this question. I would
Hello all at StackOverflow.. Reasonably new to this site, but I have had nothing
Ok, i'd like to firstly make you notice that i have looked for php
I have tried everything I can think of to get this to work, to
Ok, firstly this is not about forms this is about consistent layout as a
Firstly I am new to android and Java so this is a beginners question.
Firstly I am no longer a student and doing this for other purposes, so
Firstly could you view my outcome http://jsfiddle.net/RXnKR/ From this I decided as you click

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.