我在从array
文件中获取JSON
时遇到了一些麻烦。
示例列表:
[[1477298500000L,u' C12',u'状态'],[1477298510000L, 你好C12',无],[1477298520000L,你' C12',无],
[1477298530000L,u' X15',u' Port'],[1477298540000L,u' X15', 无],[1477298550000L,你' X15',你'状态']]
我需要在列表末尾的array
的最后一栏排序null values
:
[[1477298530000L,u' X15',u' Port'],[1477298500000L,u' C12', u'状态'],[1477298550000L,u' X15',u'状态'],
[1477298510000L,u' C12',无],[1477298520000L,u' C12', 无],[1477298540000L,u' X15',无]]
我最后也需要array
中reverse order
null values
与RecyclerView
相同:
[[1477298500000L,你' C12',你'状态'],[1477298550000L, 你' X15',你'状态'],[1477298530000L,你' X15',你'端口'],
[1477298510000L,u' C12',无],[1477298520000L,u' C12', 无],[1477298540000L,你' X15',无]]
答案 0 :(得分:0)
只需对其进行排序并反转列表:
In[33]: import operator
In[34]: test_list = [[1477298500000L, u'C12', u'Status'],
[1477298510000L, u'C12', None],
[1477298520000L, u'C12', None],
[1477298530000L, u'X15', u'Port'],
[1477298540000L, u'X15', None],
[1477298550000L, u'X15', u'Status']]
In[35]: test_list.sort(key=operator.itemgetter(2))
In[36]: test_list[::-1]
Out[36]:
[[1477298550000L, u'X15', u'Status'],
[1477298500000L, u'C12', u'Status'],
[1477298530000L, u'X15', u'Port'],
[1477298540000L, u'X15', None],
[1477298520000L, u'C12', None],
[1477298510000L, u'C12', None]]
答案 1 :(得分:0)
我通过创建比较函数解决了我自己的问题:
def make_comparator():
def compare(x, y):
if x[2] is None:
return 1
elif y[2] is None:
return -1
elif x[2]>y[2]:
return 1
elif x[2]<y[2]:
return -1
else:
return 0
return compare
def make_comparator_reverse():
def compare(x, y):
if x[2] is None:
return 1
elif y[2] is None:
return -1
elif x[2]<y[2]:
return 1
elif x[2]>y[2]:
return -1
else:
return 0
return compare