I want to fetch the name list on the basis of searching and sort lexicographically order.
I have used "like" keyword to get a similar search in the database, and I don't know how to get the required order.
func SearchByName(db *gorm.DB) func(c *gin.Context) {
return func(c *gin.Context) {
var uemail = c.Param("rexed")
var rnex []resource
//Param matches your regex //c.JSON(200,rnex)
if err := db.Where("name LIKE ?", "%uemail%").Find(&rnex).Error; err!=nil{
c.AbortWithStatus(404)
fmt.Println(err)
} else {
c.JSON(200, rnex)
}
}
}
I am getting "Mismatched *DB and string & Report incompatible types in binary and unary operation
For the first part of your question, to set the order, use the .Order()
method on gorm
db.Where("name LIKE ?", "%uemail%").Order("name").Find(&rnex)
For the second part where you're getting the error, I don't have enough context from the code you posted, but my first thought is that you're trying to parse in a value that is the wrong type in your resource
struct.