0

I have an array below and the first number in each array means order. What I want to do is, whenever I change the order, it resorts the array and re-index it into 2, 3, 4, 5.

const payments = [
    [2, paymentName1, '5%'],
    [3, paymentName2, '5%'],
    [4, paymentName3, '5%'],
    [5, paymentName4, '5%']
  ];

For example, if I change the first array order from 2 to 6, array becomes the one below.

const payments = [
    [2, paymentName2, '5%'],
    [3, paymentName3, '5%'],
    [4, paymentName4, '5%'],
    [5, paymentName1, '5%'],
  ];

what I currently did was to sort it and take for loop to re-order it. and I want to do it in one loop if possible. Please help me with writing this algorithm.

Thanks in advance!

Edit:

payments.sort((a, b) => a[0] - b[0]);

for (const index in payments) {
  payments[index][0] = parseInt(index) + 2;
}

This is my current function. Would there be a better way to do? thanks!

6
  • 1
    Show YOUR effort and where you are stuck. SO is not a service to just get your code written Commented May 27, 2021 at 17:52
  • @risingStark I added my current work Commented May 27, 2021 at 18:02
  • 1
    Why should the order start with 2? I would understand 1 or 0, but 2? Commented May 27, 2021 at 18:17
  • @trincot The ones that have an order of 0 and 1 are outside the payment array. Commented May 27, 2021 at 18:19
  • 1
    The code you've got does what you're asking just fine, but you could remove the parseInt call and just say index + 2. However, this sounds very much like an XY problem. Most likely the items in payments should be objects, not arrays, and most likely there's not a real need to sort or change indexes, if you rethink your design patterns a little. Commented May 27, 2021 at 18:31

1 Answer 1

2

After you sort, just loop over the array and assign the new order values incrementally. There is no "better" here.

const payments = [
    [2, "paymentName1", '5%'],
    [3, "paymentName2", '5%'],
    [4, "paymentName3", '5%'],
    [5, "paymentName4", '5%']
];

function setOrder(index, newOrder) {
    payments[index][0] = newOrder;
    payments.sort(([a], [b]) => a - b);
    for (let i = 0; i < payments.length; i++) payments[i][0] = i + 2;
}

setOrder(0, 6);
console.log(payments);

The time complexity is determined by the call to sort: O(nlogn).

Alternatively, you could use binary search to find the target index where the mutated element should go, and then rotate the array elements accordingly. Then the time complexity will be O(n). Although this has a better time complexity, the overhead of JavaScript code will make that for arrays of moderate sizes you'll get faster results with sort.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.