Sorting Query Results by Title in Vapor-Fluent

59 views Asked by At

Query in Fluent, I'm attempting to sort the results by title in Danish language. The current code is functional, but it seems that characters like å, æ, and ø are not being sorted correctly. Does anyone have insights on how to address this issue?

// Attraction table
final class Attraction: Model, Content {
    static let schema = "attractions"

    @ID(key: .id)
    var id: UUID?

    @Field(key: FieldKeys.title)
    var title: String

    @OptionalField(key: FieldKeys.description)
    var description: String?

    init() { }

    init(id: UUID? = nil,
         title: String,
         description: String? = nil) {
        self.id = id
        self.title = title
        self.description = description
    }

}
extension Attraction {
    enum FieldKeys {
        static let title: FieldKey = "title"
        static let description: FieldKey = "description"
    }
}


// create attraction
struct CreateAttraction: AsyncMigration {

    func prepare(on database: Database) async throws {
        try await database.schema(Attraction.schema)
            .id()
            .field(Attraction.FieldKeys.title, .string, .required)
            .field(Attraction.FieldKeys.description, .string, .required)
            .create()
    }

    func revert(on database: Database) async throws {
        try await database.schema(Attraction.schema).delete()
    }
}

// Add migration in configure file.
app.migrations.add(CreateAttraction())


struct AttractionsController : RouteCollection {
    func boot(routes: RoutesBuilder) throws {

        routes.get("attractions", use: getAttractionsHandler)
    }

    func getAttractionsHandler(_ req: Request) async throws -> [Attraction] {

        let attractions = try await .query(on: req.db)
            .sort(Attraction.FieldKeys.title)// here å, æ, and ø is not sorting proper way.
            .all()

        return attractions

    }

}

in this code i want to fix sorting danish special characters like å, æ, and ø.

0

There are 0 answers