0

I want to show a List called listaBaresFiltrada, sorted on multiple values.

Evento model:

import 'package:flutter/material.dart';

class Evento {
  final String eventoId;
  final String fechaEvento;
  final String tituloEvento;
  final String evento;
  final bool es_privado;
  final bool es_publico;
  final String num_invitados;
  final String ubicacionEvento;
  final String latitudEvento;
  final String longitudEvento;
  final String descripcionEvento;
  final String imagenEvento;
  final String autorEvento;
  final String fotoPerfilAutor;
  final String interes;
   double distancia;
   bool menosde30km;
   bool espasado;

First I would like to sort on following values:

  1. fechaEvento (DateTime ascending)

  2. menosde30km (bool true)

  3. distance (double)

For now I am able to make the firsto sorting condition, like follows:

//sorting the list
listaBaresFiltrada.sort((a,b) => 
DateFormat('d-M-yyyy HH:mm', 'de_DE').parse(a.fechaEvento).compareTo(
DateFormat('d-M-yyyy HH:mm', 'de_DE').parse(b.fechaEvento)));

But I don't know how to include both others sorting conditions.

1
  • 1
    Specifically, see my answer on the linked question which discusses sorting and subsorting on multiple properties and using a stable sort. Commented Aug 27, 2021 at 15:33

1 Answer 1

0

Since you can't sort a list with 3 different conditions I assume that you want to have 3 different list. For that you have to:

 List sortOne = listaBaresFiltrada.sort((a,b) => 
   if(a) return 1;
   else return -1
 );
 List sortTwo = listaBaresFiltrada.sort((a,b) => 
   DateFormat('d-M-yyyy HH:mm', 'de_DE').parse(a.fechaEvento).compareTo(DateFormat('d-M-yyyy HH:mm', 'de_DE').parse(b.fechaEvento))
 );
 List sortThree = listaBaresFiltrada.sort((a,b) => 
   a.distancia.compareTo(b.distancia)
 );
Sign up to request clarification or add additional context in comments.

2 Comments

Using your proposal I am getting an error: The return type 'bool' isn't a 'int', as required by the closure's context
I edited my answer. I think this is the correct answer for your question. Best luck!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.