Home java Sorting a list by field of element

Sorting a list by field of element

Author

Date

Category

There is ArrayList & LT; ModelMusic & GT; with elements>Modelmusic :

Public Class Modelmusic IMPLEMENTS SERIALIZABLE
{
  String title;
  INT COUNT;
  String SRC;
  Modelmusic (String T, Int C, String S) {
    title = t;
    Count = C;
    src = s;
  }
  Public String Gettitle () {
    Return title;
  }
  Public int getCount () {
    RETURN COUNT;
  }
  Public String Getsrc () {
    Return SRC;
  }
}

How to implement sorting by getCount () .


Answer 1, Authority 100%

Sorting Lists in Java is performed by calling the static java.util.collections.sort (List; T & GT; LIST) for lists with elements implementing java.lang.comparable & lt; T & GT; or java.util.collections.sort (List & LT; T & GT; LIST, COMPARATOR & LT ;? Super T & GT; Comparator) for lists with any items. For arrays there are similar methods in the class of java.util.arrays . Sorting changes the order of elements in the transmitted list.

Because Implementing the Comparable interface sets order on the set of all class objects, it may not be correct to set this order by the count , so I would implement a comparator:

Public Class Modelmusic IMPLEMENTS SERIALIZABLE
{
  String title;
  INT COUNT;
  String SRC;
  Modelmusic (String T, Int C, String S) {
    title = t;
    Count = C;
    src = s;
  }
  Public String Gettitle () {
    Return title;
  }
  Public int getCount () {
    RETURN COUNT;
  }
  Public String Getsrc () {
    Return SRC;
  }
  @Override
  Public String Tostring () {
    Return "ModelMusic {" +
        "title = '" + title +' \ '' +
        ", Count =" + Count +
        ", src = '" + src +' \ '' +
        '}';
  }
  Public Static Final Compararator & Lt; Modelmusic & GT; Compare_BY_COUNT = NEW COMPARATOR & LT; ModelMusic & GT; () {
    @Override
    Public Int Compare (Modelmusic LHS, Modelmusic RHS) {
      Return LHS.getCount () - RHS.Getcount ();
    }
  };
}

Here the comparator is implemented as a static field of the class, for convenience of reuse. Because count is likely to be a small non-negative number, you can not worry that when subferring, an overflow will occur, and the wrong result will be returned. If this is not the case, it is better to use integer.compare (Int LHS, INT RHS) (Available with API 16) or comparison operators.

Example of use:

arraylist & lt; modelmusic & gt; Songs = New ArrayList & lt; & gt; ();
Songs.Add (New Modelmusic ("Song 1", 8, "Song1.mp3"));
Songs.Add (New Modelmusic ("Song 2", 2, "Song2.mp3"));
Songs.Add (New ModelMusic ("Song 3", 5, "Song3.mp3"));
Songs.Add (New Modelmusic ("Song 4", 0, "Song4.mp3"));
Log.i ("sorted", "pre:" + songs.tostring ());
Collections.sort (Songs, ModelMusic.compare_BY_COUNT);
Log.i ("Sorted", "Post:" + songs.tostring ());

Conclusion:

i / sorted: pre: [modelmusic {title = 'song 1', count = 8, src = 'song1 .mp3 '}, ModelMusic {title =' Song 2 ', Count = 2, SRC =' Song2.mp3 '}, ModelMusic {title =' Song 3 ', Count = 5, SRC =' Song3.mp3 '}, ModelMusic {title = 'Song 4', Count = 0, src = 'song4.mp3'}]
I / Sorted: Post: [modelmusic {title = 'song 4', count = 0, src = 'song4.mp3'}, modelmusic {title = 'song 2', count = 2, src = 'song2.mp3'} , Modelmusic {title = 'Song 3', count = 5, src = 'song3.mp3'}, modelmusic {title = 'song 1', count = 8, src = 'song1.mp3'}]

To sort the list in the reverse order, you can use Collections.ReversErder :

collections.sort (Songs, Collections.ReversEserder (modelmusic.compare_by_count));

In Java 8, several new ways of working with comparators have been added, such as the ability to build a comparator chains for comparison over several fields, but Android does not support Java 8.


Answer 2, Authority 33%

Perhaps you need a compararator.

Public Static Void Main (String [] Args) {
    ArrayList & LT; ModelMusic & GT; modelmusicarraylist = new arraylist & lt; & gt; ();
    // Adding elements
    ModelMusicarrayList.add (New ModelMusic ("A", 1, "WHATER"));
    modelmusicarraylist.add (new modelmusic ("b", 56, "whatever"));
    modelmusicarraylist.add (new modelmusic ("C", 17, "WHATER"));
    modelmusicarraylist.add (new modelmusic ("d", 4, "whatever"));
    modelmusicarraylist.add (New ModelMusic ("E", 2, "WHATER"));
    // Sorting elements
    ModelMusicarRayList.Sort; Modelmusic & GT; () {
      @Override
      Public Int Compare (Modelmusic O1, Modelmusic O2) {
        if (o1.getcount () == o2.getcount ()) Return 0;
        ELSE if (o1.getcount () & gt; o2.getcount ()) RETURN 1;
        ELSE RETURN -1;
      }
    });
    // Element output
    for (int i = 0; i & lt; modelmusicarraylist.toarray (). Length; i ++)
    {
      System.out.PrintLN (modelmusicarraylist.get (i) .gettitle () + "" + ModelMusicarRayList.get (i) .getcount ());
    }

This code will output the following result

a 1
E 2.
D 4.
C 17.
B 56.

Answer 3, Authority 17%

Try this

Public Class Modelmusic Implements Comparable & LT; Modelmusic & GT;
{
  String title;
  INT COUNT;
  String SRC;
  Modelmusic (String T, Int C, String S) {
    title = t;
    Count = A;
    src = s;
  }
  Public String Gettitle () {
    Return title;
  }
  Public int getCount () {
    RETURN COUNT;
  }
  Public String Getsrc () {
    Return SRC;
  }
  @Override
  Public int COMPARETO (Modelmusic Othermodelmusic) {
    RETURN OTHERMODELMUSIC.GETCOUNT () - this.getcount ();
  }
}

And when you need sorting make

collections.sort (List);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions