Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda