简体   繁体   中英

How to sort an array in hyperledger fabric?

I'm working on a project which has "coinWallet" asset which has an array of coins. I want to sort (Arrange) these coins in ascending order and descending, I tried to use bubble sort algorithm but it doesn't work and I don't know why. here's my model file

namespace com.libyan.coin.network
    abstract participant User identified by id {
      o String id
      o String name

    }

    participant Bank extends User {
      o String swift optional
      o BankType bankType optional
      o Boolean Validator
    }

    participant Company extends User {
      --> Bank bankID
    }

    participant Customer extends User {
      --> Bank bankID
    }

    participant Issuer extends User {
      o String swift optional

    }

    enum BankType {
       o CommercialBank
    }

   asset CoinWallet identified by id {
      o String id 
      o Double amount 
      --> User owner
      o CoinTransaction[] Coins optional
      o String LstCoin optional

    }

    concept CoinTransaction {
      o Double amount
      o CoinTransactionType type
      o String CoinId optional
      o String prvCoin optional
      o Integer index  optional
      o Boolean Spent
    }

    enum CoinTransactionType {
      o ISSUE
      o SEND
      o RECEIVE
    }

    transaction IssueCoin {
      --> CoinWallet coinWallet
      o Double amount
     }

    transaction TransferCoin {
      --> CoinWallet sender
      --> CoinWallet receiver
      o Double amount
      o String CoinId optional
      }

    event TransactionCompleted {
      o String msg
    }
    transaction InitCustomer{}
    transaction InitBank{

and this is my Script

function onTransferCoin(transaction) {
    spentvalidation(transaction.Spent)

    if (transaction.sender.amount < transaction.amount) {
      throw new Error('Insufficient fund')
    }
    if (transaction.sender ==transaction.receiver) { ///sender and reciver can't be the same
      throw new Error('You can not send money from ur wallet to ur wallet')
    }
    var coinWallet=transaction.sender

    transaction.sender.amount -= transaction.amount
    transaction.receiver.amount += transaction.amount
     var n=coinWallet.Coins.length
     var LastCoin=coinWallet.Coins.length-1   

      var swapp;
 do {
    swapp = false;
    for (var i=0; i < n; i++)
    {
      var cntr=i,iA=i,iT=i,iC=i,iP=i,iI=i,iS=i;

      var a=coinWallet.Coins[i].amount
      var b=coinWallet.Coins[cntr++].amount

      if (a<b)
        {
          var tempA = coinWallet.Coins[i].amount;
          var tempT = coinWallet.Coins[i].type;
          var tempC = coinWallet.Coins[i].CoinId;
          var tempP = coinWallet.Coins[i].prvCoin;
          var tempI = coinWallet.Coins[i].index;
          var tempS = coinWallet.Coins[i].Spent;
           coinWallet.Coins[i].amount = coinWallet.Coins[iA++].amount;
           coinWallet.Coins[i].type = coinWallet.Coins[iT++].type;
           coinWallet.Coins[i].CoinId = coinWallet.Coins[iC++].CoinId;
           coinWallet.Coins[i].prvCoin = coinWallet.Coins[iP++].prvCoin;
           coinWallet.Coins[i].index = coinWallet.Coins[iI++].index;
           coinWallet.Coins[i].Spent = coinWallet.Coins[iS++].Spent;

           coinWallet.Coins[iA++].amount = tempA;
           coinWallet.Coins[iT++].type = tempT;
           coinWallet.Coins[iC++].CoinId = tempC;
           coinWallet.Coins[iP++].prvCoin = tempP;
           coinWallet.Coins[iI++].index = tempI;
           coinWallet.Coins[iS++].Spent = tempS;

           swapp = true;
        }
    }
    n--;
} while (swapp);

   coinWallet.LstCoin=coinWallet.Coins[LastCoin].CoinId

    var sendTransaction = getFactory().newConcept('com.libyan.coin.network', 'CoinTransaction')
    sendTransaction.amount = transaction.amount
    sendTransaction.type = "SEND"
    sendTransaction.Spent = true
    sendTransaction.CoinId = transaction.transactionId
   if (transaction.sender.Coins) {
      transaction.sender.Coins.push(sendTransaction)
    } else {
      transaction.sender.Coins= [sendTransaction]
    }

    var receiveTransaction = getFactory().newConcept('com.libyan.coin.network', 'CoinTransaction')
    receiveTransaction.amount = transaction.amount
    receiveTransaction.type = "RECEIVE"
    receiveTransaction.Spent = false
    receiveTransaction.CoinId =transaction.newTransactionId
    receiveTransaction.prvCoin =transaction.sender.LstCoin

    if (transaction.receiver.Coins) {
      transaction.receiver.Coins.push(receiveTransaction)
    } else {
      transaction.receiver.Coins = [receiveTransaction]
    }
    return getAssetRegistry('com.libyan.coin.network.CoinWallet')
      .then(function (assetRegistry) {
        return assetRegistry.updateAll([transaction.sender, transaction.receiver])
      })
      .then(function () {
        sendEvent("Transfer complete")
      })
  }

I hope I could find someone who could help me with this.

Your use of i++ everywhere is incorrect. Every time i++ is used it increments the value of i by one.

Keep the for loop using it, but ensure you also only go to less than n-1

for (var i=0; i < n - 1; i++)

then assign a variable with the value of i + 1

let next = i + 1;

replace all other uses of i++ with next (don't change the one in the for loop)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM