在 Mathematica 中按文本日期列对数组进行排序
我认为很容易的事情。
我有一个来自 CSV 的混合日期、文本和数字数据的二维列表(数组)。我希望能够按单列中的值对行进行排序,在本例中是文本格式的日期。例如:
{{1/12/2008,鲍勃,123}, {28/06/2007,爱丽丝,456}, {19/08/2009, Charlie, 789}}
我想按日期对列表中的行进行排序(以便按 Alice、Bob、Charlie 的顺序显示。)
到目前为止,我认为我可能想要在日期列中映射 DateList
并将年、月和日添加到列表中,因此它变为:
{{2008, 12, 1, Bob, 123}, {2007, 6, 28, Alice, 456}}
然后我必须进行三种排序而不是一次排序,并且需要按年份分解数组。这似乎不对,现在我被困住了。我知道这应该很简单,但我一生都无法弄清楚。任何指示表示赞赏。
谢谢,
蒂姆
Something easy I think.
I have a two dimensional list (array) of mixed date, text and numeric data sourced from a CSV. I want to be able to sort the rows by the value in a single column, which in this case is a date in text format. For example:
{{1/12/2008, Bob, 123},
{28/06/2007, Alice, 456},
{19/08/2009, Charlie, 789}}
I'd like to sort the rows in the list by the date (so that comes out in the order Alice, Bob, Charlie.)
So far I have thought that I might want to map DateList
across my date column and prepend the year, month and day to the list, so it becomes:
{{2008, 12, 1, Bob, 123}, {2007, 6, 28, Alice, 456}}
Then I'm left having to do three sorts instead of one, and needing to break the array up by year. That didn't seem right and now I'm stuck. I know this ought to be simple but I can't for the life of me figure it out. Any pointers appreciated.
Thanks,
Tim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许这...
给出了
HTH
Edit
请注意,Sort[ ] 使用 OrderedQ[ ] 进行比较,因此它可以比较列表。 (例如,更大的[ ] 则不能)。
所以,下面的代码也可以工作:
或者可能更优雅:
Perhaps this ...
gives
HTH
Edit
Note that Sort[ ] compares using OrderedQ[ ], and so it can compare lists. (Greater[ ], for example, can't).
So, the following code also works:
or perhaps more elegant:
贝利萨留第二种方法的替代方法:
An alternative to belisarius' second method: