// define a struct for database table type Product struct { ID int Name string Price float64 } // sort products by their price, in ascending order var products []Product db.Order("price").Find(&products)
// sort products by their price, in descending order var products []Product db.Order("price desc").Find(&products)Here, we use the 'Order' method with the 'price desc' parameter to sort the query result in descending order. Overall, the 'Order' method in GORM is a useful and flexible feature that allows developers to sort query results easily, enabling more efficient processing of data.