# SOLVEDReorder gradient knots by position

Hello,
I'm searching a method to get gradient Knot by position not by index, the problem with gradient->GetKnot(i) is that when user change the knots position and knots order, the knot index stay the same for each knot.

I had create the following python function to get knot by position and I sorted them from lower to higher knot position.

``````def GetKnotByPos(gradient, index):
KnotDict = {}

for i in range(knotCount) :

# dictionary’s keys select.
Ks = list(KnotDict.keys())
Ks.sort()
return KnotDict[Ks[index]]['col']

# Use
for index in range(gradient.GetKnotCount()):
color = GetKnotByPos(gradient, index)

``````

I'm searching how to do the same thing but using c++.

``````const Int32 knotCount = gradient->GetKnotCount();
for (Int32 i = 0; i < knotCount; i++)
{
const Vector color = knot.col;

result += String::VectorToString(color) + "\n";
}
``````

Hello,

you can use a maxon::BaseArray to store elements in a list. You can use maxon::BaseSort to define a class that is able to sort the elements of such a BaseArray.

In the case of GradientKnots, it could look like this:

``````// custom BaseSort based class
{
public:
static inline Bool LessThan(GradientKnot a, GradientKnot b)
{
return a.pos < b.pos;
}
};

const Int32 knotCount = gradient->GetKnotCount();

// prepare array
knots.EnsureCapacity(knotCount) iferr_return;

// fill array
for (Int32 i = 0; i < knotCount; i++)
{
knots.Append(knot) iferr_return;
}

// sort the elements stored in the array
sort.Sort(knots);

// check order
for (GradientKnot& knot : knots)
{
DiagnosticOutput("Pos: @", knot.pos);
}
``````

best wishes,
Sebastian

Hello,

you can use a maxon::BaseArray to store elements in a list. You can use maxon::BaseSort to define a class that is able to sort the elements of such a BaseArray.

In the case of GradientKnots, it could look like this:

``````// custom BaseSort based class
{
public:
static inline Bool LessThan(GradientKnot a, GradientKnot b)
{
return a.pos < b.pos;
}
};

const Int32 knotCount = gradient->GetKnotCount();

// prepare array
knots.EnsureCapacity(knotCount) iferr_return;

// fill array
for (Int32 i = 0; i < knotCount; i++)
{
knots.Append(knot) iferr_return;
}

// sort the elements stored in the array
sort.Sort(knots);

// check order
for (GradientKnot& knot : knots)
{
DiagnosticOutput("Pos: @", knot.pos);
}
``````

best wishes,
Sebastian

@s_bach

Hi Sebastian,
Perfect, thank you very much.

Best regards,
Mustapha