MCPcopy
hub / github.com/pyodide/pyodide / sort

Method sort

src/core/pyproxy.ts:1942–1984  ·  view source on GitHub ↗

* The :js:meth:`Array.sort` method sorts the elements of a * :js:class:`PyMutableSequence` in place. * @param compareFn A function that defines the sort order. * @returns A reference to the same :js:class:`PyMutableSequence`

(compareFn?: (a: any, b: any) => number)

Source from the content-addressed store, hash-verified

1940 * @returns A reference to the same :js:class:`PyMutableSequence`
1941 */
1942 sort(compareFn?: (a: any, b: any) => number): PyMutableSequence {
1943 // Copy the behavior of sort described here:
1944 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#creating_displaying_and_sorting_an_array
1945 // Yes JS sort is weird.
1946
1947 // We need this adaptor to convert from js comparison function to Python key
1948 // function.
1949 const functools = API.public_api.pyimport("functools");
1950 const cmp_to_key = functools.cmp_to_key;
1951 let cf: (a: any, b: any) => number;
1952 if (compareFn) {
1953 cf = compareFn;
1954 } else {
1955 cf = defaultCompareFunc;
1956 }
1957 // spec says arguments to compareFunc "Will never be undefined."
1958 // and undefined values should get sorted to end of list.
1959 // Make wrapper to ensure this
1960 function wrapper(a: any, b: any) {
1961 if (a === undefined && b === undefined) {
1962 return 0;
1963 }
1964 if (a === undefined) {
1965 return 1;
1966 }
1967 if (b === undefined) {
1968 return -1;
1969 }
1970 return cf(a, b);
1971 }
1972 let key;
1973 try {
1974 key = cmp_to_key(wrapper);
1975 // @ts-ignore
1976 this.$sort.callKwargs({ key });
1977 } finally {
1978 key?.destroy();
1979 cmp_to_key.destroy();
1980 functools.destroy();
1981 }
1982 // @ts-ignore
1983 return this;
1984 }
1985 /**
1986 * The :js:meth:`Array.splice` method changes the contents of a
1987 * :js:class:`PyMutableSequence` by removing or replacing existing elements and/or

Callers 3

plot_benchmark.pyFile · 0.80
initializeNativeFSFunction · 0.80
loadPackageInnerMethod · 0.80

Calls 3

pyimportMethod · 0.80
callKwargsMethod · 0.80
destroyMethod · 0.45

Tested by

no test coverage detected