  	esac
  
  	if [ -z "$portdir" -a -z "$upg_port" ]; then
-@@ -3042,11 +3194,20 @@
+@@ -3042,11 +3192,20 @@
  
  	case "$arg2" in
  	*/*)	ro_opd=$arg2 ; ro_upg_port=`iport_from_origin $ro_opd` ;;
 -	*)	if [ -d "$pdb/$arg2" ]; then
 -			ro_upg_port=$arg2
-+	*)	if [ -z "${use_pkgng}" ]; then
++	*)	if [ -z "$use_pkgng" ]; then
 +			if [ -d "$pdb/$arg2" ]; then
 +				ro_upg_port=$arg2
 +			else

 	local pkg
 	pm_v "===>>> Sorting ports by category"
 
+	if [ -n "${use_pkgng}" ]; then
+		pkg query -a "%n-%v %?d %?r" | while read pkg dep rdep; do
+			if [ $rdep -eq 0 ]; then
+				if [ $dep -eq 0 ]; then

 		    sort - $ldconfig_out | uniq -d`; do
 			temp="${temp}$file "
 		done
@@ -3711,7 +3885,12 @@
 temp=`find $LOCALBASE_COMPAT -type d -empty 2>/dev/null`
 if [ -z "$temp" ] && [ -d "$LOCALBASE_COMPAT" ]; then
 	unset files
