Sorting for string formatted number

Hi Everyone,

I am working with the data (generated by the web3 services) which include the balance as gwei, wei etc, which is quite large so I have stored them as string, now I am querying this data (paginated), but need to sort this on the field balance but as turn out it is being sorted on the character instead of the numerical value, which is quite expected but can we sort this based on the numerical value

  • I am aware that we can use aggregation but let just say we don’t want use them
const points = await this.pointModel
                .find(param, {
                    address: 1,
                    totalPoints: 1,
                    pointsPerProduct: 1,
                    _id: 0,
                })
                .sort({
                    [productId < 0 ? "totalPoints" : `pointsPerProduct.${productId}`]:
                        sortOrder === "desc" ? -1 : 1,
                })
                .skip(limit * page)
                .limit(limit)
                .lean()
                .exec()

Why not use the Decimal type for this? Storing numbers are string seems to be a way to build up a lot of issues that you’ll need to work against when accessing data:

1 Like

my only concern is about the large numbers (considering web3 in picture), but I guess you are right