java - 按值对 LinkedList 进行排序
问题描述
我有一个链表结构,每个节点都存储int ID号、字符串名称变量和节点链接变量。我想根据 ID 对这个列表进行升序排序。collection.sort 适合这个吗?我该如何实际处理?
解决方案
您有两种选择:
让你的 Node 类实现
Comparable
接口并实现compareTo(NodeType other)
如下return Integer.compare(this.id, other.id)
:在您的 LinkedList 上使用
Collections.sort
自定义比较器:Collections.sort(list, (a,b) -> Integer.compare(a.getId(),b.getId())
.
推荐阅读
- php - 如何旋转标题和字体颜色谷歌表格api的文本
- python - 有人能告诉我为什么在我的 python pytube 脚本中出现“urllib.error.HTTPError: HTTP Error 404: Not Found”吗?
- wordpress - 如何在 WooCommerce 中创建“快速商店”选项,类似于 Urban Outfitters 网站
- c# - HttpWebRequest.GetRequestStream() 不适用于 TEST 项目
- c - 单链表,main函数中的一个小问题
- angular - Typescript/Angular 12:将自定义对象转换为参数对象
- flutter - Redis Stream with Dart XADD:如何聊天
- c++ - 请帮我理解这个“包扩展不包含任何未扩展的参数包”编译器错误
- reactjs - useState 钩子和 useReactiveVar(反应变量)钩子有什么区别?
- python - 从类名中获取数据