Richard
Richard

Reputation: 164

How can I change the starting and ending items of an array while following the same order?

I would like to sort the array

order = ['B', 'E', 'A', 'D', 'G', 'C', 'F']

in a way that would return

['F', 'B', 'E', 'A', 'D', 'G', 'C']

In other words, the last element of order should be moved to the beginning and all other elements should be shifted right one position.

A new array is to be returned (order is not to be modified). How can I do that?

Upvotes: 2

Views: 46

Answers (1)

ramblex
ramblex

Reputation: 3057

Assuming your return values are subsequent outputs you can use the rotate method:

order.rotate(-1)

Upvotes: 2

Related Questions