I need to add General to last. How do I do it? I added a new column to the database. The column name is sortOrder, every other value SortOrder is null without General. SortOrder value is 1 in only General. My API json like this,
{"deF_CAT_ID": 140, "description": "General", "in_use": 1, "sortOrder": 1}
Widget createListView(BuildContext context, AsyncSnapshot snapshot) {
List<CategoryItem> values = snapshot.data;
values.sort((a,b) => a.description.toLowerCase().compareTo(b.description.toLowerCase()));
int count = 1;
return Container(
color: const Color(0xFFFFFFFF),
child: ListView.builder(
padding: EdgeInsets.only(top: 8.0, right: 0.0, left: 0.0),
itemCount: count,
itemBuilder: (BuildContext context, int index) {
return GridView.count(
physics: ScrollPhysics(),
shrinkWrap: true,
crossAxisCount: 4,
children: List.generate(values.length, (index) {
return GridTile(
child: GestureDetector(
onTap: () => sub(values[index].childId),
child: Column(
children: [
SvgPicture.asset(
'assets/images/Defect/icon-${values[index].childId}.svg',
height: 50.0,
color: Colors.blue,
),
Expanded(
child: Text(
values[index].description,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 10.0
),),),],),),);}),);},),);}
How about this? If you have multiple values having non-null sortOrder, they are sorted alphabetically.
values.sort((a, b) {
if (a.sortOrder == null && b.sortOrder != null) {
return -1;
} else if (a.sortOrder != null && b.sortOrder == null) {
return 1;
} else {
return a.description.toLowerCase().compareTo(b.description.toLowerCase());
}
});
User contributions licensed under CC BY-SA 3.0