The idea is to split the list to 3 parts and to sort each of them by version-list-< separately (but I am not sure which combination of preference is correct) and to flatten at the end
((VC packages) (builtin packages) (the rest))
I use (package-vc-p p1) and (assq p1 package--builtins) predicates for splitting (not sure for builtin). The code below needs refactoring.
(require 'cl)
(defun package--get-activatable-pkg (pkg-name)
;; Is "activatable" a word?
(let ((pkg-descs
(flatten-list
(mapcar (lambda (x)
(sort x
(lambda (p1 p2)
(let ((v1 (package-desc-version p1))
(v2 (package-desc-version p2)))
(version-list-< v2 v1)))))
(let ((y (reduce
(lambda (p1 p2)
(if (package-vc-p p1)
(push p1 (first p2))
(push p1 (second p2)))
p2)
(cdr (assq pkg-name package-alist))
:initial-value (list nil nil)
:from-end t)))
(append (list (car y))
(reduce
(lambda (p1 p2)
(if (assq p1 package--builtins)
(push p1 (first p2))
(push p1 (second p2))
p2))
(cadr y)
:initial-value (list nil nil)
:from-end t)))))))
;; Check if PACKAGE is available in `package-alist'.
(while
(when pkg-descs
(let ((available-version (package-desc-version (car pkg-descs))))
(package-disabled-p pkg-name available-version)))
(setq pkg-descs (cdr pkg-descs)))
(car pkg-descs)))