2

I am writing a program to find max possible number that can be formed from a vector of numbers in string format.

Like A = ["3" "30" "33"] should give "33330". However a normal sort using sort(A,'descend') returns "33303"

Is there any way to use a comparator function in matlab to use as key while doing sort(A) ?

Something along the lines of sort(A,key=compare_function) where my compare function would compare two strings and return which is bigger as per the above logic (for example on inputs "30" and "3" it will form two strings "303" and "330", and return that "330" is bigger as per the logic)

2
  • So do you want to sort the elements of A, or return the permutation that maximizes the concatenated number? Commented Jan 12, 2022 at 23:02
  • @LuisMendo for higher size of A , sorting in this custom way will give the required permutation Commented Jan 17, 2022 at 17:40

1 Answer 1

0

Maybe you can modify this quicksort from the File Exchange to use your own custom compare function:

https://www.mathworks.com/matlabcentral/fileexchange/66704-quicksort?s_tid=srchtitle_quicksort_1

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.