HarmonyOS 鸿蒙Next 城市对象数据本地重构排序
HarmonyOS 鸿蒙Next 城市对象数据本地重构排序
//“region_id”: 110000,
//“region_name”: “北京”
region_id: Number;
region_name: string;
constructor() {
this.region_id = 0;
this.region_name = “”;
}
}
CityDataList =CityData []
示例数据:
[
{
“region_id”: 110000,
“region_name”: “北京”
},
{
“region_id”: 110100,
“region_name”: “北京市”
},
{
“region_id”: 220100,
“region_name”: “长春市”
},
{
“region_id”: 220200,
“region_name”: “吉林市”
},
{
“region_id”: 220300,
“region_name”: “四平市”
},
{
“region_id”: 220500,
“region_name”: “通化市”
}
]
如何对 CityDataList 本地重构排序。按abc字母拍。并实现list ForEach 展示的时候。字母首次 展示出来。
B
北京
北京市
C
长春市
J
吉林市
S
四平市
T
通化市
class CarTable {
title: string = ''
projects: string[] = []
}
@Entry
@Component
struct AlphabetIndexPage {
private value: string[] = [‘选’, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’,
‘H’, ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’,
‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’,
‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’]
@State selectedIndex: number = 0
@State showDialog:boolean=false
@State isSelectIndexer: boolean = false
listScroller: ListScroller = new ListScroller()
private carList: CarTable[] = [
{
title: ‘A’,
projects: [‘奥迪’, ‘埃安’, ‘AITO’, ‘阿斯顿马丁’, ‘阿尔法罗密欧’, ‘阿维塔’, ‘爱驰’, ‘Aria’, ‘ATS’, ‘Aspark’, ‘阿尔特’, ‘AIM’]
},
{
title: ‘B’,
projects: [‘奔驰’, ‘宝马’, ‘本田’, ‘别克’, ‘比亚迪’, ‘保时捷’, ‘北京’, ‘标致’, ‘宾利’, ‘宝骏’, ‘奔腾’, ‘宝沃’, ‘布加迪’]
},
{
title: ‘C’,
projects: [‘长安’, ‘起源’, ‘长城’, ‘创维’, ‘成功’, ‘车池’, ‘刺猬’, ‘长江’, ‘Canoo’, ‘曹操’, ‘Cupra’, ‘程式’]
},
{
title: ‘D’,
projects: [‘大众’, ‘东风风行’, ‘东风风神’, ‘东风风行’, ‘东风风神’, ‘东风风行’, ‘东风风神’, ‘大发’, ‘大运’, ‘东南’, ‘DS’]
},
{
title: ‘E’,
projects: [‘Electra’, ‘E-lengend’, ‘ego’, ‘Edison’]
},
{
title: ‘F’,
projects: [‘丰田’, ‘福特’, ‘飞凡’, ‘方程’, ‘飞凡’, ‘方程’, ‘菲亚特’, ‘法拉利’]
}
]
@Builder
itemHead(text: string) {
Text(text)
.fontSize(20)
.backgroundColor(0xAABBCC)
.width(“100%”)
.padding(10)
}
build() {
Stack({ alignContent: Alignment.Start }) {
Row() {
List({ space: 0, initialIndex: 0, scroller: this.listScroller }) {
ForEach(this.carList, (item: CarTable) => {
ListItemGroup({ header: this.itemHead(item.title) }) {
ForEach(item.projects, (project: string) => {
ListItem() {
Text(project)
.width(“100%”)
.height(60)
}
}, (item: string) => item)
}
})
}
.width(‘70%’)
.height(‘100%’)
.sticky(StickyStyle.Header)
.scrollBar(BarState.Off)
.edgeEffect(EdgeEffect.None)
.onScrollIndex((start: number, end: number) => {
if (!this.isSelectIndexer) {
this.showDialog=false
this.selectedIndex = start + 1
}
})
AlphabetIndexer({ arrayValue: <span class="hljs-keyword">this</span>.value, selected: <span class="hljs-number">0</span> })
.selected(<span class="hljs-keyword">this</span>.selectedIndex)
.selectedColor(<span class="hljs-number">0xFFFFFF</span>)<span class="hljs-comment">// 选中项文本颜色</span>
.selectedBackgroundColor(<span class="hljs-number">0xCCCCCC</span>)<span class="hljs-comment">// 选中项背景颜色</span>
.usingPopup(<span class="hljs-literal">true</span>) <span class="hljs-comment">// 是否显示弹出框</span>
.selectedFont({ size: <span class="hljs-number">16</span>, weight: FontWeight.Bolder })<span class="hljs-comment">// 选中项字体样式</span>
.itemSize(<span class="hljs-number">28</span>)<span class="hljs-comment">// 每一项的尺寸大小</span>
.onSelect((index: number) => {
<span class="hljs-keyword">this</span>.showDialog = <span class="hljs-literal">true</span>
<span class="hljs-keyword">this</span>.isSelectIndexer = <span class="hljs-literal">true</span>
<span class="hljs-keyword">this</span>.listScroller.scrollToItemInGroup(index - <span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-literal">true</span>)
})
}
.width(<span class="hljs-string">'100%'</span>)
.height(<span class="hljs-string">'100%'</span>)
}
}
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
针对HarmonyOS 鸿蒙Next 城市对象数据本地重构排序的问题,以下是一些专业解答:
在HarmonyOS鸿蒙系统中,对城市对象数据进行本地重构排序通常涉及对存储在城市数据集合(如数组或Map)中的元素进行排序操作。
若数据存储在Map对象中,首先获取所有的key(城市名称或其他标识符),然后使用keySet()方法将其转换为List。接着,利用Collections.sort()或List.sort()方法对List进行排序。排序完成后,根据排序后的List重新构建一个LinkedHashMap,以保持插入顺序即排序后的顺序。
若数据存储在数组中,可以直接使用数组的sort()方法进行排序。若需要按照特定规则排序(如按城市名称的字母顺序),可以传入一个比较函数作为sort()方法的参数。
请注意,排序操作可能会因数据量的大小和排序规则的复杂度而影响性能。因此,在进行大规模数据排序时,需要考虑性能优化策略。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html