Skip to content

Commit

Permalink
sort UTXOs by value
Browse files Browse the repository at this point in the history
  • Loading branch information
pokkst committed Sep 23, 2022
1 parent 1e1fffa commit 693b342
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.mynero.wallet.service.UTXOService;

import java.util.ArrayList;
import java.util.Collections;

public class UtxosFragment extends Fragment implements CoinsInfoAdapter.CoinsInfoAdapterListener {

Expand Down Expand Up @@ -64,6 +65,7 @@ private void bindObservers(View view) {
filteredUtxos.add(coinsInfo);
}
}
Collections.sort(filteredUtxos);
if (filteredUtxos.isEmpty()) {
utxosRecyclerView.setVisibility(View.GONE);
} else {
Expand Down
15 changes: 14 additions & 1 deletion app/src/main/java/net/mynero/wallet/model/CoinsInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.ArrayList;
import java.util.List;

public class CoinsInfo implements Parcelable {
public class CoinsInfo implements Parcelable, Comparable<CoinsInfo> {
static {
System.loadLibrary("monerujo");
}
Expand Down Expand Up @@ -94,4 +94,17 @@ public int describeContents() {
public void writeToParcel(@NonNull Parcel parcel, int i) {
parcel.writeLong(globalOutputIndex);
}

@Override
public int compareTo(CoinsInfo another) {
long b1 = this.amount;
long b2 = another.amount;
if (b1 > b2) {
return -1;
} else if (b1 < b2) {
return 1;
} else {
return this.hash.compareTo(another.hash);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ArrayList<String> selectUtxos(long amount, boolean sendAll) throws Except
ArrayList<String> seenTxs = new ArrayList<>();
List<CoinsInfo> utxos = getUtxos();
long amountSelected = 0;
Collections.shuffle(utxos);
Collections.sort(utxos);
//loop through each utxo
for (CoinsInfo coinsInfo : utxos) {
if(!coinsInfo.isSpent()) { //filter out spent outputs
Expand Down

0 comments on commit 693b342

Please sign in to comment.