带有谓词的自定义排序顺序

时间:2019-10-22 21:40:15

标签: swift

我有一个数组

  

arr = [“ A”,“ B”,“ a”,“ b”,“ 1”,“ 2”]

当我使用以下方法对其进行排序时:

1 derivedAttributes)
  at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType)
  at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType)
  at System.Reflection.RuntimeAssembly.GetCustomAttributes(Type attributeType, Boolean inherit)
  at System.Attribute.GetCustomAttributes(Assembly element, Type attributeType, Boolean inherit)
  at System.Reflection.CustomAttributeExtensions.GetCustomAttributes[T](Assembly element)
  at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.GetApplicationPartAssemblies(String entryAssemblyName)
  at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateDefaultParts(String entryAssemblyName)
  at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.GetApplicationPartManager(IServiceCollection services)
  at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection services)
  at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllersCore(IServiceCollection services)
  at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllersWithViewsCore(IServiceCollection services)
  at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllersWithViews(IServiceCollection services)
  at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddMvc(IServiceCollection services)
  at Microsoft.AspNetCore.Identity.IdentityBuilderUIExtensions.AddRelatedParts(IdentityBuilder builder, UIFramework framework)
  at Microsoft.AspNetCore.Identity.IdentityBuilderUIExtensions.AddDefaultUI(IdentityBuilder builder, UIFramework framework)
  at Microsoft.AspNetCore.Identity.IdentityBuilderUIExtensions.AddDefaultUI(IdentityBuilder builder)
  at Microsoft.Extensions.DependencyInjection.IdentityServiceCollectionUIExtensions.AddDefaultIdentity[TUser](IServiceCollection services, Action

我得到:

  

[“ 1”,“ 2”,“ A”,“ B”,“ a”,“ b”]

但是我正在寻找结果:

  

[“ a”,“ b”,“ A”,“ B”,“ 1”,2“]

按自定义排序顺序排序,其中小号按排序顺序排在首位,然后按排序顺序设置上限,然后按排序顺序编号。 什么是简单的方法?

1 个答案:

答案 0 :(得分:2)

我可能是错的,但是我认为您需要实现自己的自定义排序方法:

var arr = ["A", "B", "a", "b", "1", "2"]
arr.sort {
    if $0.first?.isLowercase == true && ($1.first?.isUppercase == true || $1.first?.isWholeNumber == true) {
        return true
    }
    if $0.first?.isUppercase == true && $1.first?.isWholeNumber == true {
        return true
    }
    if $0.first?.isWholeNumber == true && ($1.first?.isLowercase == true ||  $1.first?.isUppercase == true) {
        return false
    }
    return $0 < $1
}

print(arr) // "["a", "b", "A", "B", "1", "2"]\n"

extension MutableCollection where Element: StringProtocol, Self: RandomAccessCollection {
    mutating func customSort(by areInIncreasingOrder: (Element, Element) -> Bool = (<)) {
        sort {
            if areInIncreasingOrder("1","2") {
                if $0.first?.isLowercase == true && ($1.first?.isUppercase == true || $1.first?.isWholeNumber == true) {
                    return true
                }
                if $0.first?.isUppercase == true && $1.first?.isWholeNumber == true {
                    return true
                }
                if $0.first?.isWholeNumber == true && ($1.first?.isLowercase == true ||  $1.first?.isUppercase == true) {
                    return false
                }
            } else {
                if $0.first?.isWholeNumber == true && ($1.first?.isLowercase == true ||  $1.first?.isUppercase == true) {
                    return true
                }
                if $0.first?.isUppercase == true && ($1.first?.isLowercase == true || $1.first?.isWholeNumber == true) {
                    return true
                }
                if $0.first?.isLowercase == true && ($1.first?.isUppercase == true || $1.first?.isWholeNumber == true) {
                    return false
                }
            }
            return areInIncreasingOrder($0,$1)
        }
    }
}

var array =  ["B","A","b", "a", "1", "2"]
array.customSort()

print(array) // "["a", "b", "A", "B", "1", "2"]\n"