Home > other >  How to Alpha sort the list object which has integer and strings
How to Alpha sort the list object which has integer and strings

Time:12-23

The Dart list should be sorted based on the integer value inside the element object.

Here is the list.

 List<People> items = [
  People( 10 ,  'a' ) ,
  People(  5 ,  'c' ),
  People( 15 ,  'b' ),
  People(  15 ,  'a' ),
  People(  5 , 'k' ),
  People(  10 , 'd' ) 
  People(   7, 'c' )];

Expected result :

 List<People> items = [
  People( 5 ,  'c' ) ,
  People(  5 ,  'k' ),
  People( 7 ,  'c' ),
  People(  10 ,  'a' ),
  People(  10 , 'k' ),
  People(  15 , 'a' ) 
  People(   15, 'd' )];

CodePudding user response:

Looks like the expected output for the provided input should be :

[People(5, 'c', People(5, 'k', People(7, 'c', People(10, 'a', People(10, 'd', People(15, 'a', People(15, 'b']

Try the below code to get the desired output:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) {
        ArrayList<People> people = new ArrayList<>();
        people.add(new People(10, 'a'));
        people.add(new People(5, 'c'));
        people.add(new People(15, 'b'));
        people.add(new People(15, 'a'));
        people.add(new People(5, 'k'));
        people.add(new People(10, 'd'));
        people.add(new People(7, 'c'));
        System.out.println(people);
        Collections.sort(people, Comparator.comparing(People::getNumber)
                .thenComparing(People::getaChar));
        System.out.println(people);
    }

}

class People {
    private int number;
    private char aChar;

    public People(int number, char aChar) {
        this.number = number;
        this.aChar = aChar;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public char getaChar() {
        return aChar;
    }

    public void setaChar(char aChar) {
        this.aChar = aChar;
    }

    @Override
    public String toString() {
        return "People("    number  
                ", '"   aChar  
                "'";
    }
}

CodePudding user response:

In dart language, you can sort it like below code example.

void main() {
  final items = [ People( 10 , 'a' ) , People( 5 , 'c' ), People( 15 , 'b' ), People( 15 , 'a' ), People( 5 , 'k' ), People( 10 , 'd' ), People( 7, 'c' )];
  items.sort((a,b)=> a.string.compareTo(b.string));
  items.forEach((item)=> print(item.string   item.value.toString()));
}

class People{
   final int value;
   final String string;
  
   People(this.value, this.string);
}

output;

a10
a15
b15
c5
c7
d10
k5

CodePudding user response:

Here is complete example of sorting your list

Main

void main() {
  //List of items
  List<People> items = [
    People(10, 'a'),
    People(5, 'k'),
    People(5, 'c'),
    People(15, 'b'),
    People(15, 'a'),
    People(10, 'd'),
    People(7, 'c')
  ];
  
  //Printing List before sorting
  Methods.printList(items, "\n\nBefore\n\n");
 
  //Step 01 : Sort the List according to alpha order
  items.sort((a, b) => a.string.compareTo(b.string));
  
  //Step 02 : Sort the list according to num order
  items.sort(Methods.sortMyListNumOrder);

  //Printing List after sorting
  Methods.printList(items, "\n\nAfter\n\n");
}

Method class


//class of Methods i.e printing and sorting
class Methods{
  static void printList(List<People> items, String value){
   print(value);
   for(var item in items){
      print("People(${item.integer} , ${item.string})");
    }
  }
  static int sortMyListNumOrder(People first, People second) {
    final int firstValue = first.integer;
    final int secondValue = second.integer;
    if (firstValue < secondValue) {
      return -1;
    } else if (firstValue > secondValue) {
      return 1;
    } else {
      return 0;
    }
  }
}

People Class

//People class
class People {
  final String string;
  final int integer;

  People(
    this.integer,
    this.string,
  );
}

  • Related