List<Transaction> sortTxnsByDependency(Set<Transaction> inputSet) {
        List<Transaction> result = new ArrayList<>(inputSet);
        for (int i = 0; i < result.size()-1; i++) {
            boolean txAtISpendsOtherTxInTheList;
            do {
                txAtISpendsOtherTxInTheList = false;
                for (int j = i+1; j < result.size(); j++) {
                    if (spends(result.get(i), result.get(j))) {
                        Transaction transactionAtI = result.remove(i);
                        result.add(j, transactionAtI);
                        txAtISpendsOtherTxInTheList = true;
                        break;
                    }
                }
            } while (txAtISpendsOtherTxInTheList);
        }
        return result;
    }
@Test
    public void sortTxnsByDependency() throws Exception {
        Transaction send1 = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(2, 0));
        Transaction send1a = Objects.requireNonNull(wallet.createSend(OTHER_ADDRESS, valueOf(1, 0), true));
        wallet.commitTx(send1a);
        Transaction send1b = Objects.requireNonNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 50), true));
        wallet.commitTx(send1b);
        Transaction send1c = Objects.requireNonNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 25), true));
        wallet.commitTx(send1c);
        Transaction send1d = Objects.requireNonNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 12), true));
        wallet.commitTx(send1d);
        Transaction send1e = Objects.requireNonNull(wallet.createSend(OTHER_ADDRESS, valueOf(0, 06), true));
        wallet.commitTx(send1e);

        Transaction send2 = sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, valueOf(200, 0));

        SendRequest req2a = SendRequest.to(OTHER_ADDRESS, valueOf(100, 0));
        req2a.tx.addInput(send2.getOutput(0));
        req2a.shuffleOutputs = false;
        wallet.completeTx(req2a);
        Transaction send2a = req2a.tx;

        SendRequest req2b = SendRequest.to(OTHER_ADDRESS, valueOf(50, 0));
        req2b.tx.addInput(send2a.getOutput(1));
        req2b.shuffleOutputs = false;
        wallet.completeTx(req2b);
        Transaction send2b = req2b.tx;

        SendRequest req2c = SendRequest.to(OTHER_ADDRESS, valueOf(25, 0));
        req2c.tx.addInput(send2b.getOutput(1));
        req2c.shuffleOutputs = false;
        wallet.completeTx(req2c);
        Transaction send2c = req2c.tx;

        Set<Transaction> unsortedTxns = new HashSet<>();
        unsortedTxns.add(send1a);
        unsortedTxns.add(send1b);
        unsortedTxns.add(send1c);
        unsortedTxns.add(send1d);
        unsortedTxns.add(send1e);
        unsortedTxns.add(send2a);
        unsortedTxns.add(send2b);
        unsortedTxns.add(send2c);
        List<Transaction> sortedTxns = wallet.sortTxnsByDependency(unsortedTxns);

        assertEquals(8, sortedTxns.size());
        assertTrue(sortedTxns.indexOf(send1a) < sortedTxns.indexOf(send1b));
        assertTrue(sortedTxns.indexOf(send1b) < sortedTxns.indexOf(send1c));
        assertTrue(sortedTxns.indexOf(send1c) < sortedTxns.indexOf(send1d));
        assertTrue(sortedTxns.indexOf(send1d) < sortedTxns.indexOf(send1e));
        assertTrue(sortedTxns.indexOf(send2a) < sortedTxns.indexOf(send2b));
        assertTrue(sortedTxns.indexOf(send2b) < sortedTxns.indexOf(send2c));
    }