skip to Main Content

I have a list of Week Day names in random order, how to sort it in ascending order e.g. Monday, Tuesday, Wednesday …

List

[Friday, Monday, Sunday, Wednesday]

Desired List

[Monday, Wednesday, Friday, Sunday]

I have tried

list.sort((a, b) => a.toLowerCase().compareTo(b.toLowerCase()));

3

Answers


  1. you can add mapping data with value ex

    const map = { 'Monday': 1, 'Tuesday': 2, 'Wednesday': 3, 'Thursday': 4, 'Friday': 5, 'Saturday': 6, 'Sunday': 7 };
    and then you can mapping value and sort it. or you can see in this ask
    link

    Login or Signup to reply.
  2. The issue you’re going to have to overcome is that there is no super easy way to get a listing of the days of the week. However, that isn’t actually a difficult issue to deal with if you’re only needing to deal with this for english – you can just hardcode it.

    Here’s an example:

    const weekDays = [
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday",
      "Sunday"
    ];
    
    final positions = weekDays.asMap().map((ind, day) => MapEntry(day, ind));
    

    The positions is slightly more interesting; by calling asMap, I’m converting the list to a map with the keys being the positions (i.e. 0:"Monday", … 6:"Friday") and then swapping them. This will result in a slightly faster lookup than just using indexOf, although that’s realistically probably an unnecessary optimization in this case.

    Then to sort, you can just use a list’s sort method and pass it a custom comparitor function:

    dates.sort((first, second) {
      final firstPos = positions[first] ?? 7;
      final secondPos = positions[second] ?? 7;
      return firstPos.compareTo(secondPos);
    });
    

    Putting it all together, this is what it looks like:

    const weekDays = [
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday",
      "Sunday"
    ];
    
    final positions = weekDays.asMap().map((ind, day) => MapEntry(day, ind));
    
    void main() {
      final dates = ["Friday", "Monday", "Sunday", "Banana", "Wednesday"];
    
      dates.sort((first, second) {
        final firstPos = positions[first] ?? 8;
        final secondPos = positions[second] ?? 8;
        return firstPos.compareTo(secondPos);
      });
      
      print("sorted: $dates");
    }
    

    Note that if your data isn’t complete sanitized, you might want to normalize it to be all lower case, and do the same for your "weekDays" listing.

    Login or Signup to reply.
  3. To sort the list of days such that it starts from Sunday in ascending order, you can use the sort method from the List class and specify a custom Comparator function that compares the days based on their index in the list. The code for this will be:

    https://gist.github.com/tanmoy27112000/f0077c593dd883d93a338261fd9a96a5

    This will compare the days provided with the given dayList and give you the required sorted list.

    sortedDays can also be modified to sort the given list according to your liking.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search