通过另一个列表的顺序对第二个列表进行排序。

huangapple go评论86阅读模式
英文:

Sorting a second List by the order of another

问题

我正在开发一个Flutter应用,它充当一个地址簿。我想对“names”列表进行字母排序,并对地址列表进行排序,以与姓名排序匹配,然后将它们实现到ListView中。我该如何实现第二个排序?

void main() {
    List<String> names = ["Charles", "Kevin", "George"];
    List<String> addresses = ["Western Street 2", "Roundabout 4", "Western Street 4"];
    List<String> sortedNames = sortNames(names);
}

List<String> sortNames(List<String> names) {
    return names..sort((String a, String b) => a.compareTo(b));
}

如果需要进一步的帮助,请告诉我。

英文:

I am working on a flutter app, which acts as an address book. I want to sort the 'names' list alphabetical and sort the addresses list, matching to the sorting of the names, to implement them into a listview. How can I archive this sorting of the second one?

void main() {
    List&lt;String&gt; names = [&quot;Charles&quot;,&quot;Kevin&quot;,&quot;George&quot;]
    List&lt;String&gt; adresses = [&quot;Western Street 2&quot;,&quot;Roundabout 4&quot;,&quot;Western Street 4&quot;]
    List&lt;String&gt; sortedNames = sortNames(names);
}

List&lt;String&gt; sortNames(List&lt;String&gt; names) {
    return names..sort((String a, String b)=&gt;a.compareTo(b));
}

答案1

得分: 1

这在技术上是可能的,但会比它值得的麻烦多了。尝试同时操作两个列表并确保它们的顺序始终保持一致只会导致复杂的代码和复杂的错误。

如果两个列表中的数据是相关的,并且它们之间存在一对一的关系,那么可以将数据捆绑到数据对象中,并拥有一个单一的数据对象列表。

(另外,有一种更简单的方法来对列表进行排序。)

class Person {
  Person(this.name, this.address);

  String name;
  String address;
}

void main() {
  List<Person> persons = [
    Person('Charles', 'Western Street 2'), 
    Person('Kevin', 'Roundabout 4'),
    Person('George', 'Western Street 4'),
  ];
  
  // 这种方法会直接在原始列表上排序,改变原始列表的顺序
  persons.sort((a, b) => a.name.compareTo(b.name));
  
  // 这种方法会返回一个新的列表,不会影响原始列表
  final sortedPersons = List.of(persons)..sort((a, b) => a.name.compareTo(b.name));
}
英文:

This is technically possible, but it will be more headache than it's worth. Trying to juggle two lists and ensure that their orders always line up is just asking for convoluted code and complicated bugs.

If the data in both lists is related and there is a one-to-one relationship between them, then bundle the data into data objects and have a single list of those.

(Also, there's a much easier way to sort a list.)

class Person {
  Person(this.name, this.address);

  String name;
  String address;
}

void main() {
  List&lt;Person&gt; persons = [
    Person(&#39;Charles&#39;, &#39;Western Street 2&#39;), 
    Person(&#39;Kevin&#39;, &#39;Roundabout 4&#39;),
    Person(&#39;George&#39;, &#39;Western Street 4&#39;),
  ];
  
  // This approach sorts the list in place, changing the order of the original list
  persons.sort((a, b) =&gt; a.name.compareTo(b.name));
  
  // This approach returns a new list, leaving the original unaffected
  final sortedPersons = List.of(persons)..sort((a, b) =&gt; a.name.compareTo(b.name));
}

huangapple
  • 本文由 发表于 2020年1月4日 01:11:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定