首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按文本对HtmlElements列表进行排序

按文本对HtmlElements列表进行排序
EN

Stack Overflow用户
提问于 2014-06-17 18:36:37
回答 2查看 92关注 0票数 2

我试图按List<HtmlElement>Text进行排序,文本被解析为num

代码语言:javascript
复制
 this.onClick.listen((e){
     this.parent.nextElementSibling.children.sort((Row a,Row b)=>
         num.parse(a.children[this.visibleIndex].text).
         compareTo(num.parse(b.children[this.visibleIndex].text)));
    });   

Row是一个扩展HtmlElement的类。

我得到了例外

代码语言:javascript
复制
Unsupported operation: Cannot sort element lists

周围有工作吗?还是我必须手动做?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-17 18:41:48

我还没有试过,但是我认为当添加.toList()时,它应该能工作。我认为这应该把它复制到一个可排序的标准列表中。

代码语言:javascript
复制
 this.onClick.listen((e){
     this.parent.nextElementSibling.children.toList().sort((Row a,Row b)=>
         num.parse(a.children[this.visibleIndex].text).
         compareTo(num.parse(b.children[this.visibleIndex].text)));
    });   
票数 2
EN

Stack Overflow用户

发布于 2014-09-07 13:36:46

代码语言:javascript
复制
import 'dart:html';

UListElement list;
ButtonInputElement boton;
main() {
  list = querySelector('#list'); 
  boton.onClick.listen( (e) => sort() );
}

void sort(){
  list.children = list.children.toList()
                  ..sort((LIElement x, LIElement y)=> y.text.length.compareTo(x.text.length) );  
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24271005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档