在Swift中对数组进行分组和排序

时间:2015-07-10 16:34:19

标签: arrays swift sorting

假设我有这段代码:

class Stat {
   var statEvents : [StatEvents] = []
}

struct StatEvents {
   var name: String
   var date: String
   var hours: Int
}

var currentStat = Stat()

currentStat.statEvents = [
   StatEvents(name: "lunch", date: "01-01-2015", hours: 1),
   StatEvents(name: "dinner", date: "02-01-2015", hours: 2),
   StatEvents(name: "dinner", date: "03-01-2015", hours: 3),
   StatEvents(name: "lunch", date: "04-01-2015", hours: 4),
   StatEvents(name: "dinner", date: "05-01-2015", hours: 5),
   StatEvents(name: "breakfast", date: "06-01-2015", hours: 6),
   StatEvents(name: "lunch", date: "07-01-2015", hours: 7),
   StatEvents(name: "breakfast", date: "08-01-2015", hours: 8)
]

我想知道是否有办法获得一个像这样的输出数组:

- [0]
  - name : "lunch"
  - date
    - [0] : "01-01-2015"
    - [1] : "04-01-2015"
    - [2] : "07-01-2015"
  - hours
    - [0] : 1
    - [1] : 4
    - [2] : 7     
- [1]
  - name : "dinner"
  - date
    - [0] : "02-01-2015"
    - [1] : "03-01-2015"
    - [2] : "05-01-2015"
  - hours
    - [0] : 2
    - [1] : 3   
    - [2] : 5          
- [2]
  - name : "breakfast"
  - date
    - [0] : "06-01-2015"
    - [1] : "08-01-2015"
  - hours
    - [0] : 6
    - [1] : 8 

如您所见,最终数组应按“名称”后代分组。 @oisdk你可以看一下吗?

4 个答案:

答案 0 :(得分:2)

这看起来有点矫枉过正,但这是我想到的解决方案。

extension Array {
    /**
    Indicates whether there are any elements in self that satisfy the predicate.
    If no predicate is supplied, indicates whether there are any elements in self.
    */
    func any(predicate: T -> Bool = { t in true }) -> Bool {
        for element in self {
            if predicate(element) {
                return true
            }
        }
        return false
    }

    /**
    Takes an equality comparer and returns a new array containing all the distinct elements.
    */
    func distinct(comparer: (T, T) -> Bool) -> [T] {
        var result = [T]()
        for t in self {
            // if there are no elements in the result set equal to this element, add it
            if !result.any(predicate: { comparer($0, t) }) {
                result.append(t)
            }
        }
        return result
    }
}

let result = currentStat.statEvents
    .map({ $0.name })
    .distinct(==)
    .sorted(>)
    .map({ name in currentStat.statEvents.filter({ $0.name == name }) })

现在您有一个列表列表,其中第一个列表包含晚餐类型的所有statEvents,下一个列表包含午餐类型的事件等。

明显的缺点是,这可能不如其他解决方案那么高效。好的部分是你不必依赖并行数组来获得与特定日期相关的小时数。

答案 1 :(得分:2)

已经有一些答案,但到底是什么,这很有趣。我的回答并没有在Swift中使用很多高阶函数,但它完成了工作:

// Get the list of unique event names
var eventNames = [String]()
for event in currentStat.statEvents {
    if !eventNames.contains(event.name) {
        eventNames.append(event.name)
    }
}

// The type of the result
struct ResultType {
    var name : String
    var date : [String]
    var hours : [Int]
}

var result = [ResultType]()
for name in eventNames {
    let matchingEvents = currentStat.statEvents.filter { $0.name == name }
    let dates = matchingEvents.map { $0.date }
    let hours = matchingEvents.map { $0.hours }

    result.append(ResultType(name: name, date: dates, hours: hours))
}

答案 2 :(得分:1)

最终结果是[[String:AnyObject]]类型,或者您创建一个包含这些值的新结构类型,结果类型为[String:NewStructType]:

struct NewStructType
{ 
    var dates: [String]?
    var hours: [Int]?
}

所以你必须决定那个,然后你必须编写自己的函数来对StatEvents对象进行排序和分组。也许你可以优化它的性能,但这是第一个想法如何实现第二个版本(使用NewStructType):

var result = [String : NewStructType]()

for statEvent in currentStat.statEvents
{
    if (result[statEvent.name] != nil)
    {
        var newStructType = result[statEvent.name]!

        newStructType.dates.append(statEvent.date)
        newStructType.hours.append(statEvent.hours)
    }
    else
    {
        result[statEvent.name] = NewStructType(dates: [statEvent.date], hours: [statEvent.hours])
    }
}

答案 3 :(得分:1)

我的看法:

/proc/self/status

测试:

extension StatEvents : Comparable {}

func < (lhs:StatEvents, rhs:StatEvents) -> Bool {
    if lhs.name != rhs.name {
        return lhs.name > rhs.name
    } else if lhs.date != rhs.date {
        return lhs.date < rhs.date
    } else {
        return lhs.hours < rhs.hours
    }
}

func == (lhs:StatEvents, rhs:StatEvents) -> Bool {
    return lhs.name == rhs.name
        && lhs.date == rhs.date
        && lhs.hours == rhs.hours
}

struct ResultRow {
    var name: String
    var dates: [String]
    var hours: [Int]
}

var result : [ResultRow] = []

let sorted = currentStat.statEvents.sort()
for event in sorted {
    if result.last?.name != event.name {
        result.append(ResultRow(name: event.name, dates: [], hours: []))
    }
    result[result.endIndex - 1].dates.append(event.date)
    result[result.endIndex - 1].hours.append(event.hours)
}

打印:

for r in result { print(r) }