Can I sort List<dynamic> in dart - flutter

Can I sort List<dynamic> in dart?
List<dynamic> list= [9,10,'Plus One'];
list.sort();
print(list);
I expect the result like 9,10,'Plus One' Or 'Plus One', 9, 10

You just need to provide a callback to List.sort that orders heterogeneous types the wya you want. For example, assuming that your heterogeneous List contains only ints and Strings, you could do:
List<dynamic> list = [9, 10, 'Plus One'];
list.sort((a, b) {
if ((a is int && b is int) || (a is String && b is String)) {
return a.compareTo(b);
}
if (a is int && b is String) {
return -1;
} else {
assert(a is String && b is int);
return 1;
}
});
print(list);
If you need to potentially handle other types, you will need to adjust the callback appropriately.

If you want to sort dynamic list and want string before number(int or double) try
this code:
List<dynamic> list = [
'mahmoud',
14,
'zika',
9,
10,
'plus One',
5,
'banana',
1,
2.5,
'apple',
2,
1.2,
'ball'
];
list.sort(
(a, b) {
if ((a is num && b is num) || (a is String && b is String)) {
return a.compareTo(b);
}
// a Greater than b return 1
if (a is num && b is String) {
return 1;
}
// b Greater than a return -1
else if (a is String && b is num) {
return -1;
}
// a equal b return 0
return 0;
},
);
print(list);// [apple, ball, banana, mahmoud, plus One, zika, 1, 1.2, 2, 2.5, 5, 9, 10, 14]

Related

How to join the values in-between two values in a list

I have this list [null, 3, 5, null] and I want to join the values in-between nulls and put it into the same list
For example:
[null, 3, 5, null] into [null, 35, null]
I made this one extension that groups all the values between two nulls
extension GroupByNull<T> on List<T> {
List<List<T>> groupByNull() {
final groupedList = <List<T>>[];
var list = this;
for (var i = 0; i < list.length; i++) {
if (list[i] == null) {
if (i > 0) {
groupedList.add(list.sublist(0, i));
}
list = list.sublist(i + 1);
i = 0;
}
}
if (list.isNotEmpty) {
groupedList.add(list);
}
return groupedList;
}
}
Which returns [[3, 5]] for [null, 3, 5, null]... But I want it to be joined together and added to the original list in the same index
How can I solve this...
thank you.
Note that you can't operate on an arbitrary List<T> because there's no general way to combine two elements of some arbitrary type T. What you want could make sense for List<int?> or maybe List<String?>. (Or maybe it could work on an arbitrary List<T> input if you want a List<String?> output.)
Assuming that you want to operate on List<int?>, then basically as you iterate over your input list, keep track of your current accumulated value. If you encounter null, add the current value (if any) to your output List along with the null. Don't forget to add the current accumulated value (if any) when you're done iterating in case there isn't a final null element.
extension GroupByNull on List<int?> {
List<int?> groupByNull() {
var result = <int?>[];
int? currentValue;
for (var element in this) {
if (element != null) {
currentValue = (currentValue ?? 0) * 10 + element;
} else {
if (currentValue != null) {
result.add(currentValue);
currentValue = null;
}
result.add(currentValue);
}
}
if (currentValue != null) {
result.add(currentValue);
}
return result;
}
}
void main() {
print([null, 3, 5, null].groupByNull()); // Prints: [null, 35, null]
print([3, 5, null].groupByNull()); // Prints: [35, null]
print([3, 5, null, null].groupByNull()); // Prints: [35, null, null]
print([null, 3, 5].groupByNull()); // Prints: [null, 35]
print([null, null, 3, 5].groupByNull()); // Prints: [null, null, 35]
print([null, 0, 0, 0, null].groupByNull()); // Prints: [null, 0, null]
print([null, 1, 2, null, 3, 4, null]
.groupByNull()); // Prints: [null, 12, null, 34, null]
print([null].groupByNull()); // Prints: [null]
print([null, null].groupByNull()); // Prints: [null, null]}
}
You can use this solution.
extension GroupByNull<T> on List<T> {
List groupByNull() {
final groupedList = [];
var list = this;
list.removeWhere( (value) => value == null); // remove all null values
groupedList.add(int.parse(list.join(""))); // combine number left in list
groupedList.insert(0,null); // add null
groupedList.add(null);
return groupedList;
}
}

How to random select 3 element without repeating?

Random 3 Element with repeating
How to random select 3 element without repeating?
List<Question> imageList = [
Question(
index: 1,
image: 'assets/images/1.png',
),
Question(
index: 2,
image: 'assets/images/2.png',
),
Question(
index: 3,
image: 'assets/images/3.png',
),
Question(
index: 4,
image: 'assets/images/4.png',
),
Question(
index: 5,
image: 'assets/images/5.png',
),
];
This is Element List
getRandom =
List<Question>.generate(3, (_) => imageList[random.nextInt(imageList.length)]);
This is Random Function with repeating.
You could define an extension on List<T> so you can reuse it on all types of List:
List<String> alphabets = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
extension XList<T> on List<T> {
List<T> takeRandom(int n) {
return ([...this]..shuffle()).take(n).toList();
}
}
void main() {
print(alphabets.takeRandom(3));
print(alphabets.takeRandom(5));
print(alphabets.takeRandom(7));
}
Console log
[B, S, G]
[P, M, F, Q, K]
[U, M, R, C, Z, D, G]
Get the list count;
int total = imageList.length;
create index list numbers;
var listindex = List<int>.generate(total, (i) => i++);
shuffle list;
listindex .shuffle();
//result
[3, 6, 2, 0, 9, 1, 5, 7, 8, 4]
and get data from first 3 index list.
imageList[3].image
imageList[6].image
imageList[2].image
I solved this problem
Initialized with variable
List<Question> getRandom = [];
late Question correctElement;
Random random = Random();
Init State for first time run
#override
void initState() {
setRandom();
if (kDebugMode) {
print('getRandomNum is $getRandom & correctElement is $correctElement');
}
super.initState();
}
Used do while loop to unrepeate
Question shaffel() {
return imageList[random.nextInt(imageList.length)];
}
setRandom() {
getRandom.clear();
do {
Question data = shaffel();
if (getRandom.isEmpty) {
setState(() {
getRandom.add(data);
});
} else if (getRandom.contains(data)) {
} else {
setState(() {
getRandom.add(data);
});
}
} while (getRandom.length != 3);
correctElement = getRandom[random.nextInt(getRandom.length)];
}

How to remove a certain number of duplicates from a list

I want to delete a certain number of duplicates from an ordered list in Dart. It could also be taken as the deletion of duplicates after a certain number of occurrences.
To illustrate my question, I will give an example, which could explain the problem much better than my words:
I want to keep 3 duplicates max. of each number or category.
This is what I am given:
[1,1,1,1,2,2,2,2,3,4,4,5,5,5,5,5]
Notice the occurrences per number. 3 and 4 are only present in the array one and two times correspondingly.
This is what I want that list to become:
[1,1,1,2,2,2,3,4,4,5,5,5]
void main(List<String> args) {
var numbers = [1,1,1,1,2,2,2,2,3,4,4,5,5,5,5,5];
const max_duplicates = 3;
var base = numbers.toSet();
var result = <int>[];
base.forEach((number) {
var counter = numbers.where((e) => e == number).length;
result.addAll(List.filled(counter > max_duplicates ? max_duplicates : counter, number));
});
print(result);
}
Result:
[1, 1, 1, 2, 2, 2, 3, 4, 4, 5, 5, 5]
var toRemove = [];
var localScore = 10;
var cuentaLocal = 0;
filteredCareTakers.forEach((item) {
if (localScore > item['score']) {
localScore = item['score'];
cuentaLocal = 0;
} else if (localScore == item['score']) {
if (cuentaLocal == 2) {
toRemove.add(item);
} else {
cuentaLocal++;
}
}
});
filteredCareTakers.removeWhere((element) => toRemove.contains(element));
void main() {
final input = [1, 1, 1, 1, 2, 2, 2, 2, 3, 4, 4, 5, 5, 5, 5, 5];
final seen = <Object, int>{};
var output = <Object>[];
for (var e in input) {
seen[e] = (seen[e] ?? 0) + 1;
if (seen[e]! <= 3) output.add(e);
}
print(output);
}
or for the functional programmers:
void main() {
final input = [1, 1, 1, 1, 2, 2, 2, 2, 3, 4, 4, 5, 5, 5, 5, 5];
final count = <Object, num>{};
final output = input.where((e) {
count[e] = (count[e] ?? 0) + 1;
return count[e]! <= 3;
}).toList();
print(output);
}
I imagine you could do something with .fold and and a tuple, but that just seems like too much work. :)

Get most popular value in a list

How I can get the most popular number from a list in dart without using any third party libraries?
var list = [0, 1, 1, 2, 2, 2, 3, 3, 4]; // most popular number is 2
If there are two or more popular numbers then the output should be a List with both values. Example:
One popular number:
var list = [0, 1, 1, 2, 2, 2, 3, 3, 4];
// Output should be [2]
Two or more popular numbers:
var list = [0, 1, 1, 2, 2, 2, 3, 3, 3];
// Output should be [2, 3]
Thank you in advance for your help!
This works...you can optimize it
var list = [1, 1, 2, 2, 3, 4, 5];
list.sort();
var popularNumbers = [];
List<Map<dynamic, dynamic>> data = [];
var maxOccurrence = 0;
var i = 0;
while (i < list.length) {
var number = list[i];
var occurrence = 1;
for (int j = 0; j < list.length; j++) {
if (j == i) {
continue;
}
else if (number == list[j]) {
occurrence++;
}
}
list.removeWhere((it) => it == number);
data.add({number: occurrence});
if (maxOccurrence < occurrence) {
maxOccurrence = occurrence;
}
}
data.forEach((map) {
if (map[map.keys.toList()[0]] == maxOccurrence) {
popularNumbers.add(map.keys.toList()[0]);
}
});
print(popularNumbers);
try this to count each element in list:
var list = [0, 1, 1, 2, 2, 2, 3, 3, 4];
var popular = Map();
list.forEach((l) {
if(!popular.containsKey(l)) {
popular[l] = 1;
} else {
popular[l] +=1;
}
});
I guess I found the solution.
Let me explain it to you:
I had queried through your list and checked whether the keys of the map contains the element or not. If the map does not contain the element as the key then, it will create a key from the element and pass 1 as the value. If the map does contain the element as a key then it will simply increment the value.
Once the map is ready, I had sorted the map values and stored them in a List. From the sorted map values I had taken the last element from the list of sorted values because we had sorted it in ascending order so the most popular value will be at last.
At last, I had queried through the map and check whether the value of the particular key is equal to the popularValue or not. If it is then we are adding the current key and value to the mostPopularValues list.
If I got something wrong please let me know.
void main() {
List list = [0, 1, 1, 1, 2, 2, 2, 3, 3, 4];
List mostPopularValues = [];
var map = Map();
list.forEach((element) {
if (!map.containsKey(element)) {
map[element] = 1;
} else {
map[element] += 1;
}
});
print(map);
// o/p : {0: 1, 1: 3, 2: 3, 3: 2, 4: 1}
List sortedValues = map.values.toList()..sort();
print(sortedValues);
// o/p : [1, 1, 2, 3, 3]
int popularValue = sortedValues.last;
print(popularValue);
// o/p : 3
map.forEach((k, v) {
if (v == popularValue) {
mostPopularValues.add("$k occurs $v time in the list");
}
});
print(mostPopularValues);
// o/p : [1 occurs 3 time in the list, 2 occurs 3 time in the list]
}
Not sure if that's the best solution, but it works pretty well. Let me know if there are any doubts.
final list = [0, 1, 1, 2, 2, 2, 3, 3, 4];
// Count occurrences of each item
final folded = list.fold({}, (acc, curr) {
acc[curr] = (acc[curr] ?? 0) + 1;
return acc;
}) as Map<dynamic, dynamic>;
// Sort the keys (your values) by its occurrences
final sortedKeys = folded.keys
.toList()
..sort((a, b) => folded[b].compareTo(folded[a]));
print('Most popular value: ${sortedKeys.first}'); // 1
print('Second most popular value: ${sortedKeys[1]}'); // 2
I have solved this problem by defining an extension on Iterable:
extension MostPopularItemsExtension<E> on Iterable<E> {
/// Returns the most popular items, where all items in the returned
/// list have the same number of occurances. If [this] is empty, returns an
/// empty list
///
/// Examples:
/// `[1,2,3,2].mostPopularItems() == [2]`
/// `[1,1,2,2].mostPopularItems() == [1,2]`
Iterable<E> mostPopularItems() {
if (isEmpty) return [];
final itemsCounted = <E, int>{};
for (final e in this) {
if (itemsCounted.containsKey(e)) {
itemsCounted[e] = itemsCounted[e]! + 1;
} else {
itemsCounted[e] = 1;
}
}
final highestCount = (itemsCounted.values.toList()..sort()).last;
return itemsCounted.entries
.where((e) => e.value == highestCount)
.map((e) => e.key);
}
}
The basic idea is to count all occurrences of each item in a Map object, get the highest count from this map and then return all items that have that specific number of occurrences.

Use Isolate to sort list

I have a non-primitive list which I would like to sort.
When I sort it, the UI thread is blocked and the app freezes for a few seconds.
I tried to avoid this by using dart's Isloate compute function but since the parameter sent to the compute function must be a primitive or a list/map of primitives (send method) it didn't work.
To conclude, is there any way to perform a list sort(non-primitive) without blocking the UI thread?
Edit: Clarification - I was trying to call a function through compute and I was passing a list of objects (which I got from a third party plugin) as an argument, those objects had a property of type Iterable and that caused everything to fail - make sure all the types are primitive or List/Map of primitives. With the answers I received and changing the type from Iterable to List it worked.
I'm not sure if I understood your question, but you can sort a list of non primitive elements like this:
final List<Element> elements = [
Element(id: 1),
Element(id: 7),
Element(id: 2),
Element(id: 0)
];
elements.sort((a, b) => a.compareTo(b));
// or
elements.sort((a, b) => a.id > b.id ? 1 : -1);
This would be a print(elements); output:
I/flutter ( 7351): [id: 0, id: 1, id: 2, id: 7]
And this would be the class Element
class Element {
final int id;
Element({this.id});
#override
String toString() => "id: $id";
int compareTo(Element other) => this.id > other.id ? 1 : -1;
}
Edit: To make this asynchronously, you could do this:
Future<List<Element>> asyncSort() async {
print("before sort: $elements");
elements = await compute(_sort, elements);
print("after sort: $elements");
return elements;
}
static List<Element> _sort(List<Element> list) {
list.sort((a, b) => a.compareTo(b));
return list;
}
print("before calling asyncSort(): $elements");
asyncSort();
print("after calling asyncSort(): $elements");
And this would be the output:
I/flutter ( 7351): before calling asyncSort(): [id: 1, id: 7, id: 2, id: 0]
I/flutter ( 7351): before sort: [id: 1, id: 7, id: 2, id: 0]
I/flutter ( 7351): after calling asyncSort(): [id: 1, id: 7, id: 2, id: 0]
I/flutter ( 7351): after sort: [id: 0, id: 1, id: 2, id: 7]
Edit2: If you want to send a compare function to compute, you could use a Map or a List of arguments with the list and the compare function and pass that instead of the list, because compute just takes one argument. Here is an example:
Future<List<Element>> asyncSort() async {
print("before sort: $elements");
Map args = {"list": elements, "compare": compare};
elements = await compute(_sortWith, args);
print("after sort: $elements");
return elements;
}
static List<Element> _sortWith(Map args) {
List<Element> list = args["list"];
Function(Element a, Element b) compare = args["compare"];
list.sort((a, b) => compare(a, b));
return list;
}
static int compare(Element a, Element b) {
return a.id > b.id ? 1 : -1;
}
This is how i use compute, just put all parameters to a list, then call it in a List of dynamic object:
image = await compute(getCropImage, [copyFaces, streamImg]);
imglib.Image getCropImage(List<dynamic> values) {
var face = values[0]; // copyFaces
var image = values[1]; // streamImg
}