Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add random sorting to Jig mapper #1238

Open
m0x3 opened this issue Sep 17, 2021 · 2 comments
Open

Add random sorting to Jig mapper #1238

m0x3 opened this issue Sep 17, 2021 · 2 comments

Comments

@m0x3
Copy link

m0x3 commented Sep 17, 2021

File /lib/db/jig/mapper.php
My variant is

         protected function sort($data,$cond) {
		if($cond == '_random') {
			$keys = array_keys($data);
			shuffle($keys);
			$random = array();
			foreach ($keys as $key)
				$random[$key] = $data[$key];

                        return $random;
		}
		
		$cols=\Base::instance()->split($cond);
		uasort(
			$data,
			function($val1,$val2) use($cols) {
				foreach ($cols as $col) {
					$parts=explode(' ',$col,2);
					$order=empty($parts[1])?
						SORT_ASC:
						constant($parts[1]);
					$col=$parts[0];
					if (!array_key_exists($col,$val1))
						$val1[$col]=NULL;
					if (!array_key_exists($col,$val2))
						$val2[$col]=NULL;
					list($v1,$v2)=[$val1[$col],$val2[$col]];
					if ($out=strnatcmp($v1,$v2)*
						(($order==SORT_ASC)*2-1))
						return $out;
				}
				return 0;
			}
		);
		return $data;
	}

do it like this, or please add your own better way random sorting

@Blue3957
Copy link

I wouldn't really consider shuffling to be a way to sort things. If I needed a shuffle method on my mappers, I'd probably just add one.

@joseffb-mla
Copy link

I'd want to keep with the f3 mantra and keep things light. That being said, if I needed to randomize the order I would do that in my app not the core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants