Home > Software design >  Calling object elements from a list of objects
Calling object elements from a list of objects

Time:11-12

I'm trying to figure out how to access an element of an object from a list for objects:

class ItemData {
  var id;
  var score;
  var title;
  var description;

  ItemData({this.id, this.score, this.title, this.description});
}

//list of all items
var itemList = [
  ItemData(id: 1, score: 30, title: 'mock title', description: 'mock description')
];

I am pushing instances of ItemData to itemList and then want to sort them from highest score to lowest score. I suspect I need a for loop but I can't figure out how to print the 'score' element for index[0] of itemList.

CodePudding user response:

There are two ways you can do this in my opinion. The first is to insert all your ItemData into your itemList and then call sort() on the list to get it sorted:

import 'dart:collection';

class ItemData {
  final int id;
  final int score;
  final String title;
  final String description;

  ItemData({
    required this.id,
    required this.score,
    required this.title,
    required this.description,
  });

  @override
  String toString() => '{ID: $id : Score: $score}';
}

void main() {
  final itemList = [
    ItemData(
      id: 1,
      score: 30,
      title: 'mock title',
      description: 'mock description',
    ),
    ItemData(
      id: 2,
      score: 15,
      title: 'mock title',
      description: 'mock description',
    ),
    ItemData(
      id: 3,
      score: 50,
      title: 'mock title',
      description: 'mock description',
    ),
  ];

  print(itemList);
  // [{ID: 1 : Score: 30}, {ID: 2 : Score: 15}, {ID: 3 : Score: 50}]

  itemList.sort((item1, item2) => item2.score.compareTo(item1.score));

  print(itemList);
  // [{ID: 2 : Score: 15}, {ID: 1 : Score: 30}, {ID: 3 : Score: 50}]
}

Another solution would be to use a SplayTreeSet to make a Set of ItemData which is automatically sorted as we add more ItemData to the set:

import 'dart:collection';

class ItemData {
  final int id;
  final int score;
  final String title;
  final String description;

  ItemData({
    required this.id,
    required this.score,
    required this.title,
    required this.description,
  });

  @override
  String toString() => '{ID: $id : Score: $score}';
}

void main() {
  final itemSet = SplayTreeSet<ItemData>(
    (item1, item2) => item2.score.compareTo(item1.score),
  );

  itemSet.add(
    ItemData(
      id: 1,
      score: 30,
      title: 'mock title',
      description: 'mock description',
    ),
  );
  print(itemSet);
  // {{ID: 1 : Score: 30}}

  itemSet.add(
    ItemData(
      id: 2,
      score: 15,
      title: 'mock title',
      description: 'mock description',
    ),
  );
  print(itemSet);
  // {{ID: 1 : Score: 30}, {ID: 2 : Score: 15}}

  itemSet.add(
    ItemData(
      id: 3,
      score: 50,
      title: 'mock title',
      description: 'mock description',
    ),
  );
  print(itemSet);
  // {{ID: 3 : Score: 50}, {ID: 1 : Score: 30}, {ID: 2 : Score: 15}}
}

The last solution does means your data can be put into a Set which means duplicated elements (elements which are considered equal and generates the same hashCode) will be merged.

  •  Tags:  
  • dart
  • Related