1 Replies - 506 Views - Last Post: 26 October 2017 - 01:55 PM Rate Topic: -----

#1 vcjr  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 20-September 08

Trying to Sort an Array based on Character in one of it Members

Posted 20 October 2017 - 02:31 PM

Hey, everyone, I am trying to write a program to take an array and then sort it from first to last. It is unorganized and I am trying to put it in order from when it was posted to last.
financialTransactions = [ 
  "#2 -> John -Withdrawal- ~ $756.22",
  "#5 -> Cherry +Deposit+ ~ $4,332.54",
  "#1 -> Lily +Deposit+ ~ $375.66",
  "#3 -> Noah -Withdrawal- ~ $5,244.00",
  "#4 -> Slice +Deposit+ ~ $2,305.20"
]


I am trying to get it like this.
financialTransactions[0] = "#1 -> Janira +Deposit+ ~ $375.66"
financialTransactions[1] = "#2 -> John -Withdrawal- ~ $756.22"
financialTransactions[2] = "#3 -> Noah -Withdrawal- ~ $5,244.00"
financialTransactions[3] = "#4 -> Slice +Deposit+ ~ $2,305.20"
financialTransactions[4] = "#5 -> Cherry +Deposit+ ~ $4,332.54"


The way I have been thinking of solving the problem is to first make a copy. Then scan each member in the array for its second character then if that character is not equal to the index value + 1 then it is not in its correct spot. What I would then do is replace whatever is in there with the appropriate transaction that has the corresponding index value with the second character.

Here is what I have so far but being new to this I keep getting stuck on how to properly make this work.
financialTransactions = [ 
  "#2 -> John -Withdrawal- ~ $756.22",
  "#5 -> Cherry +Deposit+ ~ $4,332.54",
  "#1 -> Janira +Deposit+ ~ $375.66",
  "#3 -> Noah -Withdrawal- ~ $5,244.00",
  "#4 -> Slice +Deposit+ ~ $2,305.20"
]

puts("Original Transactions: ")
puts(financialTransactions)


#Variables
idx = 0
organized = false
done = false
copyFinancialTransactions = []
falses = []

#Make a Copy of original - 0 < 
while idx < financialTransactions.length  
  copyFinancialTransactions.push(financialTransactions[idx])
  idx = idx + 1
end

idx = 0

while idx < financialTransactions.length
  falses.push(false)
  idx = idx + 1
end

idx = 0

while done == false
   transactionNumber = financialTransactions[idx]
   # not really needed just converted to a string so I can take the second character values -> check value 
   #puts(transactionNumber)
   if transactionNumber[idx].to_i != idx + 1
     #copy correct to original &current idx = 0(1)
     
     financialTransactions = copyFinancialTransactions[idx]
   end
     
   idx = idx + 1
   

end
idx = 0

puts("Copy of the Original")
puts(copyFinancialTransactions)

#testing an array full of falses
puts(falses)






Is This A Good Question/Topic? 0
  • +

Replies To: Trying to Sort an Array based on Character in one of it Members

#2 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4081
  • View blog
  • Posts: 18,156
  • Joined: 24-September 08

Re: Trying to Sort an Array based on Character in one of it Members

Posted 26 October 2017 - 01:55 PM

There should just be a .sort for arrays. https://ruby-doc.org...l#method-i-sort If you're forcing an index to make list or whatnot, maybe another data structure would better suit your needs.

Sorry for the delay in answering.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1