How to create a sorting algorithm that sorts like Sort[] function?

I have been trying to write a program to sort a list and cannot seem to get it right…:

Triliste[a_] :=
  Module[{min, x, i, j}, For[i = 1, i < Length[a], min := i;
      For[j = i + 1, j <= Length[a],
      If[a[[j]] < a[[min]], min := j]]; x, 
     If[min! i, x := a[[i]]; a[[i]] = a[[min]]; a[[min]] := x]]
    ] a;

what is wrong?

I noticed you write

i < Length

But Length is a function, that needs an argument. see ?Length

See this tutorial: Implement the Bubble Sort Algorithm with Patterns and Rules.

Please try this is code:

Triliste[a_] := 
 Module[{x = a}, m = Length[a]; For[i = 1, i < m, i++,For[j = i + 1, j <= m, j++, If[x[[i]] > x[[j]], x[[{i, j}]] = x[[{j, i}]]]]]; x]