1

I have an array of uint in Solidity,

How can I find the minimum element in the array using web3js?

One option is to create a getter to the array and fetch all the values, but isn't that too long with many entries?

Inspired by CryptoKitties, In the marketplace, it is possible the view the kitties sorted by "age" or "likes", how it is implemented? I could not find.

1
  • Your best bet would be to keep a record of the minimum as new elements are added, then just return that minimum. Otherwise you'll be doing an O(n) look-up. Jan 9, 2018 at 23:20

1 Answer 1

0

If you want to be able to do different kinds of sorting with the data your best bet would be to retrieve all the data from JS either with one call that gets all the data (if possible / not expecting to run out of gas) or multiple calls that get the items one at a time and then sort the array in the front-end.

1
  • 1
    This is a common sense advise. However, it doesn't apply in general. If you're implementing a decentralized exchange you need to know which is the order with lowest "ASK" inside solidity
    – Davide C
    Mar 18, 2018 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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