How to sort the terms of a polynomial?

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
349
Office Version
  1. 2003 or older
Platform
  1. Windows
I want a function to accept a polynomial string and return either an ascending or descending order sort of the polynomial terms.

For example, if the input is 87x^1+7x^5-55x^3+22x^4-94x^2-56 the expected output in descending order of the terms is 7x^5+22x^4-55x^3-94x^2+87x^1-56.

How do I sort the terms array basis the exponents of the terms?

VBA Code:
Public Function StdPoly( _
       p As String) As String

    Dim q() As String, i As Integer

    While InStr(p, " ") > 0&
        p = Replace(p, " ", "")
    Wend

    p = Replace(p, "+", " +")
    p = Replace(p, "-", " -")

    Debug.Print p

    q = Split(p, " ")

    For i = LBound(q) To UBound(q)
        Debug.Print i, q(i)
    Next i

    StdPoly = q(0)

End Function
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Forum statistics

Threads
1,214,983
Messages
6,122,598
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top