0

I have the following struct used for adding few details. Objective is to sort in descending order. Initially highest totalpoints record will be displayed. I would like to create two buttons

1) Forward - which will fetch highest totalpoints record and again if we click on the same button it should fetch the next highest totalpoints record from the struct.

2) Backward - vice-versa of above.

struct struUser{
    address userAddress;
    uint256 id;
    bytes32 originalKshoplink;
    bytes32 orginalDocumentConvention;
    bool validated;
    uint32 Points;
    uint32 totalPoints;
}

If i add the first record then it will be something like:

UserAddress: 0x000123

Id:1

originalKshoplink :http://kshoplink

originalDocumentConvention :doc1

validated:true

Points:10

totalPoints:10

If i add the second record then it will be something like:

UserAddress: 0x000123

Id:1

originalKshoplink :http://kshoplink

originalDocumentConvention :doc1

validated:true

Points:10

totalPoints:20(earlier 10points + 10points for this record)

This is for one user and i have some similar records for other user. Now i have to write a function which will return the UserAddress and his totalPoints(e.g. 100 totalPoints consider this as base).

Consider the current viewed totalPoints are 100. When i click on forward button it should fetch the next highest UserAddress and his totalPoints(e.g. 90 totalPoints), when i click again on forward button it should fetch the next highest UserAddress and his totalPoints(e.g. 80 totalPoints).

Consider the current viewed totalPoints are 80. When i click on backward button it should fetch second highest UserAddress and his totalPoints(e.g.90 totalPoints) , when i click again on backward it should fetch the third highest userAddress and his totalPoints(e.g. 100 totalPoints).

How should i achieve this in solidity? kindly let me know in case of any queries.

2

Sorting in a smart contract is going to cost you a lot of gas, especially with large arrays.

You could set up a centralized database that replicates the data in the smart contract. Then, you could have the database system do the sorting off-chain.

Users can then still verify that the data you provide is correct, because they could pull each record out of the smart contract.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.