Skip to content

Commit 9e078b4

Browse files
authored
sort by version (desc) in conda create picker (#818)
1 parent 69ff9d4 commit 9e078b4

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/managers/conda/condaUtils.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { Common, CondaStrings, PackageManagement, Pickers } from '../../common/l
3131
import { traceInfo, traceVerbose } from '../../common/logging';
3232
import { getWorkspacePersistentState } from '../../common/persistentState';
3333
import { pickProject } from '../../common/pickers/projects';
34+
import { StopWatch } from '../../common/stopWatch';
3435
import { createDeferred } from '../../common/utils/deferred';
3536
import { untildify } from '../../common/utils/pathUtils';
3637
import { isWindows } from '../../common/utils/platformUtils';
@@ -56,7 +57,6 @@ import { Installable } from '../common/types';
5657
import { shortVersion, sortEnvironments } from '../common/utils';
5758
import { CondaEnvManager } from './condaEnvManager';
5859
import { getCondaHookPs1Path, getLocalActivationScript } from './condaSourcingUtils';
59-
import { StopWatch } from '../../common/stopWatch';
6060

6161
export const CONDA_PATH_KEY = `${ENVS_EXTENSION_ID}:conda:CONDA_PATH`;
6262
export const CONDA_PREFIXES_KEY = `${ENVS_EXTENSION_ID}:conda:CONDA_PREFIXES`;
@@ -744,7 +744,6 @@ function trimVersionToMajorMinor(version: string): string {
744744
const match = version.match(/^(\d+\.\d+\.\d+)/);
745745
return match ? match[1] : version;
746746
}
747-
748747
export async function pickPythonVersion(
749748
api: PythonEnvironmentApi,
750749
token?: CancellationToken,
@@ -757,11 +756,25 @@ export async function pickPythonVersion(
757756
.filter(Boolean)
758757
.map((v) => trimVersionToMajorMinor(v)), // cut to 3 digits
759758
),
760-
)
761-
.sort()
762-
.reverse();
763-
if (!versions) {
764-
versions = ['3.11', '3.10', '3.9', '3.12', '3.13'];
759+
);
760+
761+
// Sort versions by major version (descending), ignoring minor/patch for simplicity
762+
const parseMajorMinor = (v: string) => {
763+
const m = v.match(/^(\d+)(?:\.(\d+))?/);
764+
return { major: m ? Number(m[1]) : 0, minor: m && m[2] ? Number(m[2]) : 0 };
765+
};
766+
767+
versions = versions.sort((a, b) => {
768+
const pa = parseMajorMinor(a);
769+
const pb = parseMajorMinor(b);
770+
if (pa.major !== pb.major) {
771+
return pb.major - pa.major;
772+
} // desc by major
773+
return pb.minor - pa.minor; // desc by minor
774+
});
775+
776+
if (!versions || versions.length === 0) {
777+
versions = ['3.13', '3.12', '3.11', '3.10', '3.9'];
765778
}
766779
const items: QuickPickItem[] = versions.map((v) => ({
767780
label: v === RECOMMENDED_CONDA_PYTHON ? `$(star-full) Python` : 'Python',

0 commit comments

Comments
 (0)