在 Mathematica 中按文本日期列对数组进行排序

发布于 2024-10-06 18:40:54 字数 500 浏览 1 评论 0原文

我认为很容易的事情。

我有一个来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤千羽 2024-10-13 18:40:54

也许这...

l = {{"1/12/2008", Bob, 123}, {"28/06/2007", Alice, 456}, 
     {"19/08/2009", Charlie, 789}}

SortBy[l, AbsoluteTime[{#[[1]], {"Day", "Month", "Year"}}] &]   

给出了

{{"28/06/2007", Alice,   456}, 
 {"1/12/2008",  Bob,     123}, 
 {"19/08/2009", Charlie, 789}}   

HTH

Edit

请注意,Sort[ ] 使用 OrderedQ[ ] 进行比较,因此它可以比较列表。 (例如,更大的[ ] 则不能)。

所以,下面的代码也可以工作:

Sort@(Flatten@{DateList[{#[[1]],{"Day","Month","Year"}}], #[[2]], #[[3]]} & /@ l)

或者可能更优雅:

Sort@(l/.{x_String, y__} :> Flatten@{DateList[{x, {"Day", "Month", "Year"}}], y})

Perhaps this ...

l = {{"1/12/2008", Bob, 123}, {"28/06/2007", Alice, 456}, 
     {"19/08/2009", Charlie, 789}}

SortBy[l, AbsoluteTime[{#[[1]], {"Day", "Month", "Year"}}] &]   

gives

{{"28/06/2007", Alice,   456}, 
 {"1/12/2008",  Bob,     123}, 
 {"19/08/2009", Charlie, 789}}   

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:

Sort@(Flatten@{DateList[{#[[1]],{"Day","Month","Year"}}], #[[2]], #[[3]]} & /@ l)

or perhaps more elegant:

Sort@(l/.{x_String, y__} :> Flatten@{DateList[{x, {"Day", "Month", "Year"}}], y})
溺孤伤于心 2024-10-13 18:40:54

贝利萨留第二种方法的替代方法:

lst = {{"1/12/2008", Bob, 123},
       {"28/06/2007", Alice, 456}, 
       {"19/08/2009", Charlie, 789}};

lst = {DateList@{#, {"Day", "Month", "Year"}}, ##2} & @@@ lst;

Sort@lst

An alternative to belisarius' second method:

lst = {{"1/12/2008", Bob, 123},
       {"28/06/2007", Alice, 456}, 
       {"19/08/2009", Charlie, 789}};

lst = {DateList@{#, {"Day", "Month", "Year"}}, ##2} & @@@ lst;

Sort@lst
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文