This question already has answers here:
Sort a list of objects in Flutter (Dart) by property value
(12 answers)
Closed 3 years ago.
I need to sort a list in Flutter, that's an object List, I have a model and that model has a property named as isFeatured, I need that all the elements with isFeatured as true be in the first positions of the List.
I mean I could have something like:
[
{
id: 1,
name: 'Test',
isFeatured: false,
},
{
id: 2,
name: 'Test 3',
isFeatured: true,
},
{
id: 3,
name: 'Test 5',
isFeatured: false,
},
{
id: 4,
name: 'Test 34',
isFeatured: true,
}
]
And the elements with isFeatured as true should be in the first position.
you can use sort method with custom comparator for bool
myList.sort((a, b) => (a.isFeature ? 1 : 0) - (b.isFeature ? 1 : 0));
Use List.sort
var myList = [
{
'id': 1,
'name': 'Test',
'isFeatured': false,
},
{
'id': 2,
'name': 'Test 3',
'isFeatured': true,
},
{
'id': 3,
'name': 'Test 5',
'isFeatured': false,
},
{
'id': 4,
'name': 'Test 34',
'isFeatured': true,
}
];
myList.sort((a, b) => (b['isFeatured']?1:0).compareTo(a['isFeatured']?1:0));
Related
I made three lists, the first for free servers, the second for paid servers, and the third list includes both together.
The problem is that when he adds the lists in the third list, he adds them in order
In other words, he adds the whole first list above and the second list below
I want a way to combine the two lists and arrange them according to name, number, or any other thing. The important thing is that they are not arranged according to the same list (the first is above and the second is below)
You can use sort method on 'allservs'. Here is a working sample that sorts combined list based on 'cnumber':
var servers = List.from([
{
'name': 'Singapore',
'flag': 'assets/images/flags/singapore.png',
'cnumber': 7,
'isvip': false,
},
{
'name': 'Singapore',
'flag': 'assets/images/flags/singapore.png',
'cnumber': 2,
'isvip': false,
},
{
'name': 'Singapore',
'flag': 'assets/images/flags/singapore.png',
'cnumber': 1,
'isvip': false,
},
]);
var newServers = List.from([
{
'name': 'Singapore',
'flag': 'assets/images/flags/singapore.png',
'cnumber': 7,
'isvip': false,
},
{
'name': 'Singapore',
'flag': 'assets/images/flags/singapore.png',
'cnumber': 12,
'isvip': false,
},
{
'name': 'Singapore',
'flag': 'assets/images/flags/singapore.png',
'cnumber': 3,
'isvip': false,
},
]);
get allServers => (List.from(newServers) + List.from(servers))
..sort((a, b) => (a['cnumber'] >= b['cnumber']) ? 1 : -1);
void main() {
print(allServers);
}
The simples way would be to do it like this:
var allServers = [...servers, ...newsServers ];
For Example, you can see this sample in DartPad:
image
which would result in outputing:
[{name: Singapore}, {name1: Singapore1}]
I have a User collection with an array of favorite products
User {
favoriteProducts: [1, 2, 3, 4]
}
now I have query that fetch 10 products at a time so how can I check which one of these products are in user favoriteProducts list. there must be a way with aggregation framework and $setIntersection
the output must be some thing like this
[
{
_id: 1,
name: "productA",
isFavorite: false,
},
{
_id: 2,
name: "productB",
isFavorite: true,
},
{
_id: 3,
name: "productC",
isFavorite: true,
},
{
_id: 4,
name: "productD",
isFavorite: false,
},
]
This question already has answers here:
MongoDB field order and document position change after update
(8 answers)
Closed 5 years ago.
I have this object and query
Object
{
_id: 1,
name: 'John Smith',
items: [{
name: 'item 1',
value: 'one',
_id: 1
},{
name: 'item 2'
value: 'two',
_id: 2,
}]
}
Query
Person.update({'items._id': 2}, {'$set': {
'items.$.surname': 'new surname'
}}, function(err) { ...
I want that the new updated (key / value) to be setted on first position like this
,{
surname: 'new surname',
name: 'item 2'
value: 'two',
_id: 2,
}
not like this
,{
name: 'item 2',
value: 'two',
_id: 2,
surname: 'new surname',
}
So is there a solution,
Thanks in advance
As far as I know if you append a property to an object that wasn't previously there it will always be appended to the object. The only workaround is to set the surname in the object to start with.
I have two lists and i'm trying to combine them to a new list so that the existing ids are updated and the new ones are added to list and after that sorted by the id. Is there a better or more efficient way to do this?
// Original list
const list = Immutable.List([
{ id: 1, name: 'List Item 1' },
{ id: 2, name: 'List Item 2' },
{ id: 3, name: 'List Item 3' },
]);
// One updated item and two new items
const newList = Immutable.List([
{ id: 2, name: 'Updated List Item 2' },
{ id: 4, name: 'New List Item 4' },
{ id: 5, name: 'New List Item 5' },
]);
// Get updated ids
const ids = newList.map((item) => item.id);
// Filter out updated ids from orignial list
const filteredList = list.filterNot(item => ids.includes(item.id));
// Concat and sort by id
const concatList = newList
.concat(filteredList)
.sortBy(item => item.id);
console.log(concatList.toJS());
/* Outputs as desired
[
{ id: 1, name: "List Item 1" },
{ id: 2, name: "Updated List Item 2" },
{ id: 3, name: "List Item 3" },
{ id: 4, name: "New List Item 4" },
{ id: 5, name: "New List Item 5" }
]
*/
This is how I would do it, using reduce and merge:
function reduceToMap(result, item) { return result.set(item.id, item) }
const list = Immutable.List([
{ id: 1, name: 'List Item 1' },
{ id: 2, name: 'List Item 2' },
{ id: 3, name: 'List Item 3' },
]).reduce(reduceToMap, Immutable.Map());
// One updated item and two new items
const newList = Immutable.List([
{ id: 2, name: 'Updated List Item 2' },
{ id: 4, name: 'New List Item 4' },
{ id: 5, name: 'New List Item 5' },
]).reduce(reduceToMap, Immutable.Map());
console.log(...list.merge(newList).values())
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.js"></script>
I am using the Sencha Touch 2.0 KitchenSink example to try to learn some of the basics. I am getting stuck on chained selectfields though.
I am going off the tutorial here but with the different setup I am being thrown off. The code below runs perfectly in the Forms.js file in kitchensink, but I am missing on the actual chainedselect part of it.
EDIT: What I am asking is how to make chained selectfields. I have the datastores and the selectfields in the example code, but not a working chained selectfield from first to second.
Ext.regModel('First', {
idProperty: 'FirstID',
fields: [{
name: 'FirstID',
type: 'int'
}, {
name: 'FirstName',
type: 'string'
}]
});
Ext.regModel('Second', {
idProperty: 'SecondID',
fields: [{
name: 'SecondID',
type: 'int'
},{
name: 'FirstID',
type: 'int'
}, {
name: 'SecondName',
type: 'string'
}]
});
var firstStore = new Ext.data.Store({
model: 'First',
data: [{
FirstID: 1,
FirstName: 'Kenworth'
}, {
FirstID: 2,
FirstName: 'Peterbilt'
}],
autoLoad: true
});
var secondStore = new Ext.data.Store({
model: 'First',
data: [{
SecondID: 1,
FirstID: 1,
SecondName: 'T800'
}, {
SecondID: 2,
FirstID: 1,
SecondName: 'T700'
}, {
SecondID: 3,
FirstID: 1,
SecondName: 'T660'
}, {
SecondID: 4,
FirstID: 1,
SecondName: 'T470'
}],
autoLoad: true
});
Ext.define('Kitchensink.view.Forms', {
extend: 'Ext.tab.Panel',
requires: [
'Ext.form.Panel',
'Ext.form.FieldSet',
'Ext.field.Number',
'Ext.field.Spinner',
'Ext.field.DatePicker',
'Ext.field.Select',
'Ext.field.Hidden'
],
config: {
activeItem: 0,
tabBar: {
// docked: 'bottom',
ui: 'dark',
layout: {
pack: 'center'
}
},
items: [
{
title: 'Basic',
xtype: 'formpanel',
id: 'basicform',
iconCls: 'refresh',
items: [
{
xtype: 'fieldset',
title: 'Enter Data',
instructions: 'Please enter the information above.',
defaults: {
labelWidth: '35%'
},
items: [
{
xtype: 'selectfield',
name: 'firstfield',
label: 'First',
store: firstStore,
displayField: 'FirstName',
valueField: 'FirstID'
}, {
xtype: 'selectfield',
name: 'secondfield',
label: 'Second',
store: secondStore,
displayField: 'SecondName',
valueField: 'SecondID'
}
]
}
]
}
]
},
onFirstChange: function(selectField, value){
var secondSelectField = this.items.get(1);
secondSelectField.store.clearFilter(); // remove the previous filter
// Apply the selected Country's ID as the new Filter
secondSelectField.store.filter('FirstID', value);
// Select the first City in the List if there is one, otherwise set the value to an empty string
var firstValue = secondSelectField.store.getAt(0);
if(firstValue){
secondSelectField.setValue(firstValue.data.SecondID);
} else {
secondSelectField.setValue('');
}
}
});