排序日期从壁橱到现在的第一和过去在底部 [英] Sort dates with closets to now first and past at the bottom

查看:92
本文介绍了排序日期从壁橱到现在的第一和过去在底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格视图,其中有多个日期.我需要对日期进行排序,以使降序最大的日期在顶部((indexPath.row == 0)),而过去的日期必须在底部((indexPath.row == ´lastRows´))在其他即将到来的日期之间.

I have a tableview in which there are multiple date. I need to sort the dates so that the most descending date is at the top ((indexPath.row == 0)) and the past dates needs to be at the bottom ((indexPath.row == ´lastRows´)) and in between the other upcoming dates.

我一直在四处寻找找不到合适的解决方案.以下代码仅将最早的日期显示在第一位-未能按照我的期望将过去的日期放在底部. (有关问题的更清晰示例,请参见图片:

I have been looking around without finding a proper solution. The following code only displays the earliest date first - failing to put the past dates at the bottom as I desire. (see picture for clearer example of the problem, the sort I want:

  • 即将到来的人
  • 即将到来的2
  • 过去的日期 )

  • Upcoming One
  • Upcoming 2
  • Past Date )

dqItems.sort(by: {$0.date < $1.date})

有没有办法做到这一点?或者,当日期成为过去时,将日期(及其单元格)推到表格视图的底部吗?

Is there a way of accomplishing this? Alternatively pushing a date (and its cell) to the bottom of the table view when the date becomes the past?

推荐答案

不是使用.sort,而是使用.sorted获取排序后的副本.然后搜索排序后的副本以查找当前时间所在的位置.使用该索引制作几个切片,并将切片复制到所需的位置(返回原始数组).

Rather than use .sort, use .sorted to get a sorted copy. Then search the sorted copy to find where the current time belongs. Use that index to make a couple of slices and copy the slices where you want them (back in the original array).

这篇关于排序日期从壁橱到现在的第一和过去在底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆