Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
Sorting lists with one instance

Hello I am trying to pass to my jsp page two sorted Lists which I get from the database like that

   @RequestMapping(value = "/sortDate", method= RequestMethod.GET)
  public void sortedLists(Model model, HttpSession session){


    List<Song> songsByDate;
    try {
    songsByDate = SongDAO.getInstance().getAllSongs();
    Collections.sort(songsByDate, new UploadTimeComparator());
    session.setAttribute("songs", songsByDate);
     /////////////////////////////////
    List<Song> songsByLikes;
    try {
        songsByLikes = SongDAO.getInstance().getAllSongs();
    Collections.sort(songsByLikes, new LikesComparator());
    session.setAttribute("songs2", songsByLikes);
   }}}

However the problem is since I use getInstance() , songs and songs2 are the same Lists sorted by Likes,how can I overcome this problem?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to copy each "all songs" list to a new array list, which you can then proceed to sort.

So:

List<Song> songsByDate = new ArrayList<>( SongDAO.getInstance().getAllSongs() );
Collections.sort( songsByDate, new UploadTimeComparator() );

List<Song> songsByLikes = new ArrayList<>( SongDAO.getInstance().getAllSongs() );
Collections.sort( songsByLikes, new LikesComparator() );
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!