Home > OS >  Flutter sorted function doesn't sort correctly
Flutter sorted function doesn't sort correctly

Time:09-28

I'm trying to sort a list of objects by their timestamp, but as you can see below from the timestamps the list that gets returned is just randomized. Does anyone know what issue im facing here?

var sortedFixtures = fixtures.sorted((a, b) =>
    a.fixtureDetails?.timestamp ??
    0.compareTo(b.fixtureDetails?.timestamp ?? 0));

This produces the following (the timestamp for each team is underneath the name):

I/flutter (30585): West Ham
I/flutter (30585): 1664641800
I/flutter (30585): Southampton
I/flutter (30585): 1664632800
I/flutter (30585): Manchester City
I/flutter (30585): 1664715600
I/flutter (30585): Liverpool
I/flutter (30585): 1664632800
I/flutter (30585): Leicester
I/flutter (30585): 1664823600
I/flutter (30585): Leeds
I/flutter (30585): 1664724600
I/flutter (30585): Fulham
I/flutter (30585): 1664632800
I/flutter (30585): Crystal Palace
I/flutter (30585): 1664632800
I/flutter (30585): Arsenal
I/flutter (30585): 1664623800
I/flutter (30585): Bournemouth
I/flutter (30585): 1664632800

CodePudding user response:

You might be missing a bracket

var sortedFixtures = fixtures.sorted((a, b) =>
    (a.fixtureDetails?.timestamp ?? 0).compareTo(b.fixtureDetails?.timestamp ?? 0));
  • Related