Skip to content

Instantly share code, notes, and snippets.

@benznest
Created July 28, 2020 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benznest/37766daca77f6b12029d9ac5efb93b52 to your computer and use it in GitHub Desktop.
Save benznest/37766daca77f6b12029d9ac5efb93b52 to your computer and use it in GitHub Desktop.
class Product{
String title;
bool isSale;
int price;
Product({this.title,this.isSale,this.price});
toString(){
return "title = $title, isSale = $isSale, price = $price";
}
}
void main() {
List<Product> products = List.generate(30,(i){
return Product(title: "No.$i",isSale: i % 4 ==0,price: (i * 1000) );
});
products.sort((a,b){
if((a.isSale && b.isSale) || (!a.isSale && !b.isSale) ){
if(a.price > b.price){
return -1;
}else if(a.price == b.price){
return 0;
}else{
return 1;
}
}else if (a.isSale && !b.isSale){
return -1;
}
else{ // !a.isSale && b.isSale
return 1;
}
});
products.forEach((p){
print(p.toString());
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment